std-method

Result::map

Transform a successful value while returning an existing Err unchanged. The method consumes the Result and leaves the error type E unchanged. Expected stdout (not executed): Ok(12) Common mistake: Using map to transform the error payload.

Formal Syntax & Structure

rust syntaxrust
fn map<U, F: FnOnce(T) -> U>(self, op: F) -> Result<U,E>

Working Example

Example Snippetrust
fn main() {    let quantity = "4".parse::<u32>().map(|n| n * 3);    println!("{quantity:?}");}
Rust Reference