Rustlings: Live!

How much structs could a rust struct struct

Goals:

Share knowledge

Ask Questions

Pair

Grow

Structuring Data in Rust

What is a `Struct`

Structs are similar to tuples. Like tuples, the pieces of a struct can be different types. Unlike with tuples, you’ll name each piece of data so it’s clear what the values mean.

A `struct `can also technically be "tuple-like"... I know.

Using Structs

Using Structs

struct BlackTea {
    name: String,
    tea_type: String,
    temperature_in_celcius: f32,
    ingredients: Vec<String>,
}

Using Structs

let earl_grey = BlackTea {
    name: String::from("Earl Grey"),
    tea_type: String::from("Assam"),
    temperature_in_celcius: 100.0,
    ingredients: vec![
        "assam".to_string(),
        "bergamot".to_string()
    ]
};

Using Structs: Get values

println!(
    "{} is a type of black tea.
    Often made with {}",
    earl_grey.name,
    earl_grey.tea_type
);

Using Structs: Get vec elements

println!(
    "{} is a blend of {}, {}",
    earl_grey.name,
    earl_grey.ingredients[0],
    earl_grey.ingredients[1]
);

Printing Structs

Printing Structs

Using Structs: Update instance

let earl_grey = BlackTea {
    name: String::from("Earl Grey"),
    temperature_in_celcius: 100.0,
    ingredients: vec![
        String::from("assam"),
        String::from("bergamot")
    ]
};

earl_grey.tea_type = String::from("keemun");

Using Structs: Update instance

let earl_grey = Tea {
    name: String::from("Earl Grey"),
    tea_type: String::from("black"),
    temperature_in_celcius: 100.0,
    ingredients: vec![
        String::from("assam"),
        String::from("bergamot")
    ]
};

let earl_green = Tea {
    name: String::from("Russian Earl Grey"),
    ..earl_grey
}

Tuple Structs

struct Location(f32, f32);

let rust = Location(48.267799, 7.731100)

Unit Structs

struct Temperature;

let temp = Temperature

Structs in a method

struct Rectangle {
    width: i32,
    height: i32,
}

 

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

let r1 = Rectangle { width: 30, height: 50, };

What is an `enum`?

An enumeration, also referred to as enum is a simultaneous definition of a nominal enumerated type as well as a set of constructors, that can be used to create or pattern-match values of the corresponding enumerated type.

Using Enums

enum Teas {
    BlackTea,
    GreenTea,
    WhiteTea
}

 

struct BlackTea {
    ...
}

 

struct GreenTea {
    ...
}

Using Enums

enum Leaf {
    Sinensis
    Assamica,
}

struct BlackTea {
    name: String,
    variety: Leaf,
    tea_type: String,
    temperature_in_celcius: f32,
    ingredients: Vec<String>,
}

Using Enums

let earl_grey = BlackTea {
    name: String::from("Earl Grey"),
    variety: Leaf::Assamica,
    tea_type: String::from("Assam"),
    temperature_in_celcius: 100.0,
    ingredients: vec![String::from("Bergamot")]
};

Using Enums

match earl_grey.variety {
    Leaf::Assamica => {
        println!(
          "{} is a type of Camellia assamica", 
          earl_grey.name
        )
    },
    _ => println!(
           "{} is another type of leaf", 
           earl_grey.name
         ),
}

Using Enums

Using Enums

enum SteepTemp {
    Green(f32)
    Black(f32),
}
impl SteepTemp{
   fn get_temperature(&self)->f32{
      match *self{
          SteepTemp::Black(value)=> value,
          SteepTemp::Green(value)=> value
      }
   }
}

Using Enums

Let's Pair!

Rustlings

https://github.com/rust-lang/rustlings

Rustlings

Structs and Enums

$ rustlings run structs1 
$ rustlings hint structs1
Complete Exercises 
Struct 1, 2, 3 & Enum 1, 2, 3

Rustlings: Live!

By shortdiv

Rustlings: Live!

  • 589