Additionally, creating a function that can take any type of Machine as its parameter is not possible using structs; in such a case, enums must be used. With an enum, the type of machine can be identified inside the function by using pattern matching with the match keyword.
The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.
An enum in Rust is a type that represents data that is one of several possible variants. Each variant in the enum can optionally have data associated with it: #![allow(unused_variables)] fn main() { enum Message { Quit, ChangeColor(i32, i32, i32), Move { x: i32, y: i32 }, Write(String), }
You could implement std::fmt::Display for IpAddr or match the enum in your print function, see print_ip2 snippet. fn print_ip2(ip: Option<IpAddr>) { match ip { Some(IpAddr::V4(val)) => println!( "{}", val), Some(IpAddr::V6(val)) => println!( "{}", val), None => println!(
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
".
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
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.
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.
For a simple retrieval of value, you can use "if let"
let mut var: f64 = 0.0;
if let Shape::Circle(_, float1) = my_shape {
var = float1;
}
println!("value is {}", var);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With