gec*_*kos 1 pattern-matching rust
我正在阅读https://www.abubalay.com/blog/2018/04/08/recursive-ascent,它做了我无法理解的事情
let Elements(array) = elements;
Run Code Online (Sandbox Code Playgroud)
这种表达是什么意思?它在创建array
变量吗?那Elements(...)
!!
问候
这是一项破坏性任务。它只是从结构或枚举中取值:
struct Elements(Vec<&'static str>);
fn main() {
let elements = Elements(vec!["hello", "world"]);
let Elements(array) = elements;
println!("array: {:?}", array); // prints array: ["hello", "world"]
}
Run Code Online (Sandbox Code Playgroud)
您可以在Rust的许多地方使用模式。即使在let语句中。只要确保该模式是无可辩驳的。