O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mapping of the Option/Result values

The map and map_err methods provide a way to concisely apply mapping functions on the contents of the values inside Ok/Some and Err values. Since doing anything with None values would be pointless, map_err is not defined for Option. Here are the full types of these methods:

map<U, F>(self, f: F) -> Result<U, E>     where F: FnOnce(T) -> U map<U, F>(self, f: F) -> Option<U>     where F: FnOnce(T) -> U map_err<F, O>(self, f: O) -> Result<T, F>     where O: FnOnce(E) -> F 

Reading through the types carefully, we see that the map functions for both Result and Option types take a function that transforms a value of type T to a value of type U, that is, the FnOnce declaration. The return type tells us that the new value ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required