3 ms·
Even if you were using pre-processed data, pattern matching helps to make the code easier to read. I guess I can think of clearer examples in other languages, l
by dguaraglia 6y ago
Even if you were using pre-processed data, pattern matching helps to make the code easier to read. I guess I can think of clearer examples in other languages, like Rust, that supported tagged unions:
// Given this enum
enum NumOrStr {
Num(i32),
Str(string)
}
// That allows you to define values like this
let x = NumOrStr::Num(10);
let y = NumOrStr::Str("hello");
// And some random code that produces a value z that is of type NumOrStr
let z: NumOrStr = ...
match z {
Num(50) => println!("Got my favorite number: fifty!"),
Num(n) => println!('Got {}', n),
String("hello") => say_hello(),
String(s) => do_something_with_string(s)
_ => println!("Default case, x = {:?}", x),
}