Skip to content

Irrefutable patterns #291

Open
Open
@jonathanpallant

Description

@jonathanpallant

It might be worth adding a couple of bonus slides into Compound Types on irrefutable patterns, and how they can be used with let and also as function parameters.

pub struct Weight(f32);

pub fn process_weight(Weight(kg): Weight) {
   println!("Weight is {kg:.03}");
}

pub fn process_weight2(w: Weight) {
   let Weight(kg) = w;
   println!("Weight is {kg:.03}");
}

pub fn make_pair() -> (Weight, Weight) {
   (Weight(1.0), Weight(2.0))
}

fn main() {
   let (x, y) = make_pair(); 
   process_weight(x);
   process_weight2(y);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions