我在阅读一些 Rust 代码时遇到了这一行
if let Some(path) = env::args().nth(1) {
这个函数的内部
fn main() {
if let Some(path) = env::args().nth(1) {
// Try reading the file provided by the path.
let mut file = File::open(path).expect("Failed reading file.");
let mut content = String::new();
file.read_to_string(&mut content);
perform_conversion(content.as_str()).expect("Conversion failed.");
} else {
println!(
"provide a path to a .cue file to be converted into a MusicBrainz compatible tracklist."
)
}
}
该行似乎将 env
参数分配给变量路径,但我无法弄清楚它周围的 Some()
正在做什么。
我查看了 Option
的文档我理解它在 =
右侧使用时的工作原理,但在左侧我有点困惑。
我认为这条线等同于
if let path = Some(env::args().nth(1)) {
最佳答案
来自reference :
An if let expression is semantically similar to an if expression but in place of a condition expression it expects the keyword let followed by a refutable pattern, an = and an expression. If the value of the expression on the right hand side of the = matches the pattern, the corresponding block will execute, otherwise flow proceeds to the following else block if it exists. Like if expressions, if let expressions have a value determined by the block that is evaluated.
这里重要的部分是可反驳性。它的意思是可反驳的模式在这里它可以有不同的形式。例如:
enum Test {
First(String, i32, usize),
Second(i32, usize),
Third(i32),
}
您可以检查 x 的值以获得 3 种不同模式的值,例如:
fn main() {
let x = Test::Second(14, 55);
if let Test::First(a, b, c) = x {}
if let Test::Second(a, b) = x {} //This block will be executed
if let Test::Third(a) = x {}
}
这称为可反驳性。但是考虑一下您的代码:
enum Test {
Second(i32, usize),
}
fn main() {
let x = Test::Second(14, 55);
if let Test::Second(a, b) = x {}
}
此代码无法编译,因为 x 的模式很明显,它只有一个模式。 您可以从 reference of refutability 获得更多信息。 .
另外你的想法是不对的:
if let path = Some(env::args().nth(1)) {
编译器会抛出类似irrefutable if-let pattern 的错误,因为正如引用所说:“关键字 let 后跟一个可反驳的模式”。这里“让”之后没有可反驳的模式。实际上这段代码试图创建一个名为 path 的变量,它是一个选项,这没有任何意义,因为不需要“If”,
相反,Rust 希望你这样写:
let path = Some(env::args().nth(1)); // This will be seem like Some(Some(value))
关于syntax - Some() 在变量赋值的左侧做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901550/