2017-01-15 11 views
3

我有这样一个枚举:如何解决“警告:未使用的变量”的枚举在Rust中的命名参数?

pub enum Tag<'a> { 
    Container { c: Vec<Tag<'a>> }, 
    // ... 
} 

,当我尝试匹配:

impl<'a> Display for Tag<'a> { 
    fn fmt(&self, f: &mut Formatter) -> fmt::Result { 
     match *self { 
      Tag::Container { ref c } => write!(f, "{}", "container"), 
      // ... 
     } 
    } 
} 

我得到:

warning: unused variable: `c`, #[warn(unused_variables)] on by default 
    | 
65 |    Tag::Container{ref c} => write!(f, "{}", "container"), 

和其他一些地方。

我试图使用_,只是删除ref c,但它都会导致错误。

回答

6

您可以使用..

match *self { 
    Tag::Container { .. } => write!(f, "{}", "container"), 

这是覆盖在书中,特别是在Ignoring bindings它是用于忽略包裹在一个枚举变形值:

enum OptionalTuple { 
    Value(i32, i32, i32), 
    Missing, 
} 

let x = OptionalTuple::Value(5, -2, 3); 

match x { 
    OptionalTuple::Value(..) => println!("Got a tuple!"), 
    OptionalTuple::Missing => println!("No such luck."), 
}