Algebraic Data Types & Pattern Matching

  • product types as structs 1
  • sum types as enums 1
enum Option<T> {
  None,
  Some(T),
}

fn test(values: Option<&Vec<u32>>) -> String {
  match values {
    Option::None =>
      "none".to_string(),
    Option::Some(v) if v.len() > 3 =>
      "3+".to_string(),
    Option::Some(v) =>
      v.len().to_string(),
  }
}

fn main() {
  let v = vec![1, 2, 3, 4];
  println!("{}", test(Option::Some(&v)));
}