A filesystem path
This is a non-owning variant (a view). For the owned path buffer, see PathBuf.
Example
use std::fs::Path;
let p = Path::new("/home/user/file.txt");
assert!(p.is_absolute());
assert_eq!(p.parent(), Option::some(Path::new("/home/user")));
Run this example
Fields
-
inner: &[u8]
Methods
impl Path { ... }
-
Create a path from a string slice
Example
use std::fs::Path; let _ = Path::new("/home/user/file.txt");
Run this example -
fn iter(self: &Path) -> PathIterator
-
Returns
true
if the path is absolute,false
otherwiseExample
use std::fs::Path; assert!(Path::new("/home/user/file.txt").is_absolute()); assert!(!Path::new("file.txt").is_absolute());
Run this example -
Returns
true
if the path is relative,false
otherwiseExample
use std::fs::Path; assert!(Path::new("file.txt").is_relative()); assert!(!Path::new("/home/user/file.txt").is_relative());
Run this example -
Returns the parent path of the path
If the path has no parent, then
Option::none()
is returned.Example
use std::fs::Path; assert_eq!(Path::new("/home/user/file.txt").parent(), Option::some(Path::new("/home/user"))); assert_eq!(Path::new("/home/user").parent(), Option::some(Path::new("/home"))); assert_eq!(Path::new("/home").parent(), Option::some(Path::new("/"))); assert_eq!(Path::new("/").parent(), Option::none());
Run this example -
Strips the prefix from the path.
If the path does not start with the prefix, then
Option::none()
is returned.Example
use std::fs::Path; let p = Path::new("/home/user/file.txt"); let stripped1 = p.strip_prefix(Path::new("/home/user")); let stripped2 = p.strip_prefix(Path::new("/something/else")); assert_eq!(stripped1, Option::some(Path::new("file.txt"))); assert_eq!(stripped2, Option::none());
Run this example -
Returns
true
ifother
is a prefix of the current path,false
otherwiseExample
use std::fs::Path; let p = Path::new("/home/user/file.txt"); assert!(p.starts_with(Path::new("/home/user"))); assert!(!p.starts_with(Path::new("/something/else")));
Run this example
Mixins
impl Path { ... }
-
-
Returns
false
if arguments are equal,true
otherwise