November 2017
Intermediate to advanced
264 pages
5h 45m
English
File paths from the underlying file system are represented by the Path struct, which is created from a string slice containing the full path with Path::new. Suppose hello.txt is an existing file in the current directory; let's write some code to explore it:
// code from Chapter 11/code/paths.rs:use std::path::Path;
fn main() {
let path = Path::new("hello.txt");
let display = path.display();
// test whether path exists:
if path.exists() {
println!("{} exists", display);
}
else {
panic!("This path or file does not exist!");
}
let file = path.file_name().unwrap(); let extension = path.extension().unwrap(); let parent_dir = path.parent().unwrap(); println!("This is file {:?} with extension {:?} in folder {:?}", file, extension, parent_dir); ...Read now
Unlock full access