Skip to Main Content
Rust Programming Cookbook
book

Rust Programming Cookbook

by Claus Matzinger
October 2019
Intermediate to advanced content levelIntermediate to advanced
444 pages
10h 37m
English
Packt Publishing
Content preview from Rust Programming Cookbook

How to do it...

Just a few steps need to be followed in order to learn more about pattern matching:

  1. Create a new binary project using cargo new pattern-matching. This time, we'll run an actual executable! Again, open the project using Visual Studio Code or another editor.
  2. Let's check out literal matching. Just like a switch-case statement in other languages, each matching arm can match to literals as well:
fn literal_match(choice: usize) -> String {    match choice {        0 | 1 => "zero or one".to_owned(),        2 ... 9 => "two to nine".to_owned(),        10 => "ten".to_owned(),        _ => "anything else".to_owned()    }}
  1. However, pattern matching is way more powerful than that. For example, tuple elements can be extracted and selectively matched:
fn tuple_match(choices: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rust Web Programming

Rust Web Programming

Maxwell Flitton
The Complete Rust Programming Reference Guide

The Complete Rust Programming Reference Guide

Rahul Sharma, Vesa Kaihlavirta, Claus Matzinger

Publisher Resources

ISBN: 9781789530667Supplemental Content