Rust 编程语言中的 Path 结构
Rust 中的路径结构用于表示底层文件系统中的文件路径。还应注意,Rust 中的路径不表示为 UTF-8 字符串;相反,它存储为字节向量(Vec<u8>)。
示例
考虑下面显示的示例 -
use std::path::Path;
fn main() {
// Create a `Path` from an `&'static str`
let path = Path::new(".");
// The `display` method returns a `Show`able structure
let display = path.display();
// Check if the path exists
if path.exists() {
println!("{} exists", display);
}
// Check if the path is a file
if path.is_file() {
println!("{} is a file", display);
}
// Check if the path is a directory
if path.is_dir() {
println!("{} is a directory", display);
}
// `join` merges a path with a byte container using the OS specific
// separator, and returns the new path
let new_path = path.join("a").join("b");
// Convert the path into a string slice
match new_path.to_str() {
None => panic!("new path is not a valid UTF-8 sequence"),
Some(s) => println!("new path is {}", s),
}
}输出
如果我们运行上述代码,我们将看到以下输出 -
. exists . is a directory new path is ./a/b
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP