Rust tips is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Rust tips @rust

A file path in can be a simple UTF-8 string (String/&str), or a string in an OS-dependent encoding (OsString/OsStr), or a wrapper type specifically for manipulating paths (PathBuf/&Path).

With the help of `AsRef` trait you can write functions that seamlessly work with all of these types:

fn takes_path<P: AsRef<Path>>(path: P) {
let path = path.as_ref();
// Now the path is always just &Path
}

Try it: is.gd/01ZiqK

· Web · 0 · 3