rust - Matching on a Box -
this code 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 run it, following error occurs:
error[e0554]: #[feature] may not used on stable release channel
i tried
fn main() { let b = some(box 5); }
error: box expression syntax experimental;
is because version of rust not latest? how can content in box::new()
? tried
fn main() { let b = some(box::new(5)); match b { some(box::new(y)) => print!("{:?}", y), _ => print!("{:?}", 1), } }
error[e0164]: `box::new` not name tuple variant or tuple struct --> main.rs:6:14 | 6 | some(box::new(y)) => print!("{:?}", y), | ^^^^^^^^^^^ not tuple variant or struct
you using #[feature]
, can used nightly rust compiler. don't think possible match against box
in stable rust, nightly allows following way of doing (like attempted in beginning):
#![feature(box_patterns)] fn main() { let b = some(box::new(5)); match b { some(box y) => print!("{:?}", y), _ => print!("{:?}", 1), } }
Comments
Post a Comment