This code is shown in 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!(),
}
}
But when I run it, the following error occurs:
error[E0554]: #[feature] may not be used on the stable release channel
I also tried
fn main() {
let b = Some(box 5);
}
error: box expression syntax is experimental;
Is it because my version of Rust is not the latest? How I can get the content in Box::new()
? I tried
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
syntax, but didn't read the intro to chapter 6 entitled Nightly Rust that describes the first 2/3 of your question? – Pubilis