I am very new to learning Rust language. Would some one here could explain following:
match number {
if number % 2 == 0 => println!("even), \\ it does not work
x if number % 2 ==0 => println!("even), \\ it works
}
As I googled it, the AI answer is that " The Rust code match number { if number % 2 == 0 => println!(""), } doesn't work because you cannot use if statements directly within the arms of a match expression; instead, you should use patterns that match the value"
My question is that why I cannot use 'if' within the arms of 'match'?
Match requires that each arm starts with a pattern. A simple "x" is regarded as a pattern, but "if number % 2 == 0" is not. Your second arm uses a match guard, see
Hi Stephen, thanks for answering! My second questions in this context are too basic, may not be the right forum to ask.(too silly).
Anyway, the question is in the RBE, it is indicated that " A matchguard can be added to filter the arm". What exactly is it meant/used within match arms in the given example?
The second question is : regarding pattern, what exactly it is? Per Marriam-Webster, " "pattern" can be defined as a form or model proposed for imitation..."
As I look at The Rust Reference, it says, "Patterns are used to match values against structures and to, optionally, bind variables to values inside these structures. They are also used in variable declarations and parameters for functions and closures." It does not even have any semblance of dictionary definition.
Sorry, I can not explain you all the details. When I started learning Rust, for me the pattern matching was one of the hardest parts to learn. I read about it in various books for a long time and carefully, and still have problems with it, e.g. remembering the details and exact syntax. I would recommend you to read it somewhere as well. I tried to describe it as good as I was able to in Patterns and pattern matching - Rust for C-Programmers, but I put it close to the end of the book, as I think pattern matching is not something that is needed that early for a Rust beginner. (Well, simple forms are introduced earlier in the book already.) I would compare it to regular expressions (RegEx) -- when you learn a language, it is not immediately needed to understand RegEx, even when regular expressions can be very helpful. The official book, or "Programming Rust" by Jim Blandy should explain all the details of pattern matching well.
Thanks for pointing to the book. The book has wealth of information about 'match' and 'pattern'.
I feel, the 'match' is similar to 'when' in Kotlin language. 'when' is little more flexible. Anyway, thanks once again for taking time and answering to my posting!
Note that number must be a valid identifier at this point anyway. This won't compile:
In
let number = 12;
match "foo" {
x if number % 2 == 0 => println!("even"),
_ => println!("odd"),
}
First if number % 2 == 0 is being evaluated and then "foo" is moved into x.
Of course number must be a valid identifier at this point.
Also the fact, that we match against a random string is confusing to say the least.
You can see this behavior if the type used in the match statement isn't Copy: