Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "warning: unused variable" for enum with named params in Rust?

I have an enum like:

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

when I try to match:

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

I get:

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

and in some other places.

I tried to use _, just to remove ref c but it all causes errors.

like image 377
Sergey Avatar asked Jan 15 '17 21:01

Sergey


1 Answers

You can use ..:

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

This is covered in the book, specifically under Ignoring bindings where it is used to ignore the values wrapped in an enum variant:

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."),
}
like image 144
Simon Whitehead Avatar answered Oct 19 '22 05:10

Simon Whitehead