以下代码摘自 The Rust Programming Language:
#![feature(box_syntax, box_patterns)]
fn main() {
let b = Some(box 5);
match b {
Some(box n) if n < 0 => {
println!("Box contains negative number {}", n);
}
Some(box n) if n >= 0 => {
println!("Box contains non-negative number {}", n);
}
None => {
println!("No box");
}
_ => unreachable!(),
}
}
但是当我运行时,出现了以下错误:
error[E0554]: #[feature] may not be used on the stable release channel
我也尝试过。
fn main() {
let b = Some(box 5);
}
error: box expression syntax is experimental;
是因为我的Rust版本不是最新的吗?我怎样才能获取Box::new()
中的内容呢?我尝试过
fn main() {
let b = Some(Box::new(5));
match b {
Some(Box::new(y)) => print!("{:?}", y),
_ => print!("{:?}", 1),
}
}
error[E0164]: `Box::new` does not name a tuple variant or a tuple struct
--> main.rs:6:14
|
6 | Some(Box::new(y)) => print!("{:?}", y),
| ^^^^^^^^^^^ not a tuple variant or struct
box
语法的部分,但是你没有阅读第6章介绍夜间版Rust的内容,其中描述了你问题的前2/3部分? - Shepmaster