January 2019
Beginner to intermediate
554 pages
13h 31m
English
Rust's match expressions are quite a joy to use. It's basically C's switch statement on steroids and allows you to make decisions, depending on what value the variable has and whether it has advanced filtering capabilities. Here's a program that uses match expressions:
// match_expression.rsfn req_status() -> u32 { 200}fn main() { let status = req_status(); match status { 200 => println!("Success"), 404 => println!("Not Found"), other => { println!("Request failed with code: {}", other); // get response from cache } }}
In the preceding code, we have a req_status, function that returns a dummy HTTP request status code of 200, which we call in main and assign to status. We then match on this value using the match keyword, ...
Read now
Unlock full access