How do I use the box keyword in pattern matching?

后端 未结 2 408
轻奢々
轻奢々 2021-01-12 14:25

This code is shown in The Rust Programming Language:

#![feature(box_syntax, box_patterns)]

fn main() {
    let b = Some(box 5);
    match b {
        Some(b         


        
2条回答
  •  情书的邮戳
    2021-01-12 14:54

    You are using a #[feature] and those can only be used with a nightly Rust compiler. I don't think it is currently possible to match against a Box in stable Rust, but nightly allows the following way of doing it (like you attempted in the beginning):

    #![feature(box_patterns)]
    
    fn main() {
        let b = Some(Box::new(5));
        match b {
            Some(box y) => print!("{:?}", y),
            _ => print!("{:?}", 1),
        }
    }
    

提交回复
热议问题