January 2018
Beginner to intermediate
454 pages
10h 8m
English
Just like the previous section, this will show you some tips to make your API more comfortable to use by auto-converting it into a Path.
So, let's take an example with a function receiving a Path as an argument:
use std::path::Path;
fn some_func(p: &Path) {
// some code...
}
There's nothing new in here. You can call this function just like this:
some_func(Path::new("tortuga.txt"));
The annoying thing, here, is that you have to build the Path yourself before sending it to the function. This is way too annoying, but we can do better!
fn some_func<P: AsRef<Path>>(p: P) {
// some code...
}
And that's it... You can now call the function as follows:
some_func(Path::new("tortuga.txt")); // If you *really* like to build the ...Read now
Unlock full access