Hello everyone,
I'm new to rust and at the moment I was reading the rust book. So my question is about first edition of the book where Error Handling is discussed.
In the book the map function is defined
fn map<F, T, A>(option: Option<T>, f: F) -> Option<A> where F: FnOnce(T) -> A {
match option {
None => None,
Some(value) => Some(f(value)),
}
}
and later it is called in the next example
fn extension(file_name: &str) -> Option<&str> {
find(file_name, '.').map(|i| &file_name[i+1..])
}
Later in the same section a similar example is given
fn unwrap_or<T>(option: Option<T>, default: T) -> T {
match option {
None => default,
Some(value) => value,
}
}
and the call
fn main() {
assert_eq!(extension("foobar.csv").unwrap_or("rs"), "csv");
assert_eq!(extension("foobar").unwrap_or("rs"), "rs");
}
Since the find
function in the first example returns the Option<T>
type as well as the extension
function, their values are then passed into the map
and assert_eq!
.
I know that when I implement some methods for some Struct I pass either self
, &self
or &mut self
into the function and how the things are working is clear. However, in these two examples there is no self and consequently I would expect to call the map
as
map(find(file_name, '.'), |i| &file_name[i+1..])
I could not find anything about it in the documentation.
Thanks for the help.