Rust绑定
间接访问变量使得不可能分支和使用该变量,而无需重新绑定。匹配提供@印记就是出于这样的目的:
// A function `age` which returns a `u32`. fn age() -> u32 { 15 } fn main() { println!("Tell me type of person you are"); match age() { 0 => println!("I'm not born yet I guess"), // Could `match` 1 ... 12 directly but then what age // would the child be? Instead, bind to `n` for the // sequence of 1 .. 12. Now the age can be reported. n @ 1 ... 12 => println!("I'm a child of age {:?}", n), n @ 13 ... 19 => println!("I'm a teen of age {:?}", n), // Nothing bound. Return the result. n => println!("I'm an old person of age {:?}", n), } }
本站文章除注明转载外,均为本站原创或编译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:代码驿站 [http:/www.codeinn.net]
本文标题:Rust绑定
本文地址:http://www.codeinn.net/rust/1328.html
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创优秀实例教程
转载请注明:文章转载自:代码驿站 [http:/www.codeinn.net]
本文标题:Rust绑定
本文地址:http://www.codeinn.net/rust/1328.html