How do you access enum values in Rust?

后端 未结 4 1286
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 12:02
struct Point {
    x: f64,
    y: f64,
}

enum Shape {
    Circle(Point, f64),
    Rectangle(Point, Point),
}

let my_shape = Shape::Circle(Point { x: 0.0, y: 0.0 },         


        
相关标签:
4条回答
  • 2020-11-28 12:10

    As you are only interested in matching one of the variants, you can use an if let expression instead of a match:

    struct Point {
        x: f64,
        y: f64,
    }
    
    enum Shape {
        Circle(Point, f64),
        Rectangle(Point, Point),
    }
    
    fn main() {
        let my_shape = Shape::Circle(Point { x: 0.0, y: 0.0 }, 10.0);
    
        if let Shape::Circle(_, radius) = my_shape {
            println!("value: {}", radius);
        }
    }
    

    This means "if my_shape can be destructured into a Circle, do nothing with the first index, but bind the value of the second index to radius".

    0 讨论(0)
  • 2020-11-28 12:16

    You can use pattern matching:

    struct Point {
        x: f64,
        y: f64,
    }
    
    enum Shape {
        Circle(Point, f64),
        Rectangle(Point, Point),
    }
    
    fn main() {
        let my_shape = Shape::Circle(Point { x: 0.0, y: 0.0 }, 10.0);
    
        match my_shape {
            Shape::Circle(_, value) => println!("value: {}", value),
            _ => println!("Something else"),
        }
    }
    

    Example output:

    value: 10
    
    0 讨论(0)
  • 2020-11-28 12:23

    Here is another way to do it:

    struct Point {
        x: f64,
        y: f64,
    }
    
    enum Shape {
        Circle(Point, f64),
    }
    
    fn main() {
        let Shape::Circle(_, radius) = Shape::Circle(Point { x: 0.0, y: 0.0 }, 10.0);
        println!("value: {}", radius);
    }
    

    This only works if the pattern is irrefutable, such as when the enum type you're matching on only has one variant. To make this work, I had to remove the unused Rectangle variant.

    In cases where you have more than one variant, you'll probably want the full match expression anyway, since you're presumably handling more than just one kind of shape.

    0 讨论(0)
  • 2020-11-28 12:29

    From The Rust Programming Language:

    Another useful feature of match arms is that they can bind to parts of the values that match the pattern. This is how we can extract values out of enum variants.

    [...]

    fn value_in_cents(coin: Coin) -> u32 {
        match coin {
            Coin::Penny => 1,
            Coin::Nickel => 5,
            Coin::Dime => 10,
            Coin::Quarter(state) => {
                println!("State quarter from {:?}!", state);
                25
            },
        }
    }
    

    If you'd like to be able to write functions that are capable of working on multiple types with different representations, have a look at traits.

    0 讨论(0)
提交回复
热议问题