typestar

Enum with match in Rust

An enum with data in its variants, matched exhaustively.

enum Shape {
    Circle(f64),
    Rect(f64, f64),
}

fn area(shape: &Shape) -> f64 {
    match shape {
        Shape::Circle(r) => std::f64::consts::PI * r * r,
        Shape::Rect(w, h) => w * h,
    }
}

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
172
Tokens
66
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 44 seconds.

Type this snippet

Step 2 of 4 in Pattern matching, step 20 of 39 in Language basics.

← Previous Next →