Common Data Structures
Enumeration

An enumeration, or enum, is a data type that allows a variable to take on one of several possible values (or states). Each of these possibilities is called a "variant." For example, in Rust, an enum can represent different states of a traffic light with variants such as Red
, Yellow
, and Green
.
enum Direction {
Up,
Down,
Left,
Right,
}
fn which_way(go: Direction) -> &'static str {
match go {
Direction::Up => "up",
Direction::Down => "down",
Direction::Left => "left",
Direction::Right => "right",
}
}
Structure

struct ShippingBox {
depth: i32,
width: i32,
height: i32,
}
fn main() {
let my_box = ShippingBox {
depth: 3,
width: 2,
height: 5,
};
let tall = my_box.height;
println!("the box is {:?} units tall", tall);
}
Last updated