Transform the contained Ok value with a function.
The map method on Result<T, E> applies a function to the contained Ok value, leaving an Err value untouched. It returns a new Result with the transformed Ok type.
This method enables transforming success values without explicit pattern matching while preserving error information. It is chainable with other Result combinators.
map is particularly useful in chains of operations where you want to transform the success path without affecting error handling.
| Name | Description | Optional |
|---|---|---|
f |
A function or closure to apply to the Ok value. | No |
let x: Result<i32, &str> = Ok(5);
assert_eq!(x.map(|v| v * 2), Ok(10));
let y: Result<i32, &str> = Err("fail");
assert_eq!(y.map(|v| v * 2), Err("fail"));