January 2018
Beginner to intermediate
454 pages
10h 8m
English
So how can we know which variant is in a variable whose type is an enumeration and how to get the values out of it? For that, we need to use pattern matching. The match expression is one way to do pattern matching. Let's see how to use it to compute the result of an expression:
fn print_expr(expr: Expr) { match expr { Expr::Null => println!("No value"), Expr::Add(x, y) => println!("{}", x + y), Expr::Sub(x, y) => println!("{}", x - y), Expr::Mul(x, y) => println!("{}", x * y), Expr::Div { dividend: x, divisor: 0 } => println!("Divisor is zero"), Expr::Div { dividend: x, divisor: y } => println!("{}", x/y), Expr::Val(x) => println!("{}", x), } }
A match expression is a way to check whether a value follows a certain pattern ...
Read now
Unlock full access