Rust Programming Tutorial #29 - Pattern Matching (Switch Statement)

preview_player
Показать описание
Similar to a 'switch statement' you might see in other languages, the Match Operator in Rust allows you to do different things based on the value of something else. You can use it to compare numbers, strings etc.

In this example we look at numbers and strings, as well as ranges and or's - it is very powerful.

It is based on 'patterns' in Rust, for more info:

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

range pattern '...' is deprecated, use '..=' for this

IdiocracyIsAProphecy
Автор

Hye Dom, so far i've liked your tutorial a lot, however, i have a questions. What's the difference between defining a string like let x = String::from("Domenic"); and let x = "Domenic"? Does the latter is the native one? Or how it works? Thanks in advance.

Paacocs
Автор

Match is like a Switch Statement yes. But much much more powerfull. It's a switch statement on steroids. Pattern matching.

HermanWillems
Автор

Hi Dom, I get this error:

error[E0658]: exclusive range pattern syntax is experimental (see issue #37854)
--> tut29_match.rs:8:3
|
8 | 2..20 => println!("It is greater than or equal to 12 and lte 20!"),
|

error: aborting due to previous error

I'm running rustc version 1.27.0

It appears that the expression exclusive range patterns have been deprecated?

Steve-P
Автор

A bit out of date... For the 3...20 => range part you need to change it to

3..=20 =>

JD-srzh
Автор

1...20 false. 1..=20 true in the last version.

JoseLuisTabaraCarbajo