Questions tagged [match-ergonomics]
6 questions
14
votes
1 answer
Match ergonomics and & pattern
Consider following code
fn main() {
let s = (&&0,);
let (x,) = s; // &&i32
let (&y,) = s; // &i32
let (&&z,) = s; // i32
let t = &(&0,);
let (x,) = t; // &&i32
let (&y,) = t; // i32
let u = &&(0,);
let (x,) = u;…

ZeroXbot
- 163
- 1
- 5
10
votes
1 answer
Why does pattern matching on &Option yield something of type Some(&T)?
I have a tiny playground example here
fn main() {
let l = Some(3);
match &l {
None => {}
Some(_x) => {} // x is of type &i32
}
}
I'm pattern matching on &Option and if I use Some(x) as a branch, why is x of type &i32?

nz_21
- 6,140
- 7
- 34
- 80
7
votes
1 answer
What does pattern-matching a non-reference against a reference do in Rust?
What happens when pattern-matching against a reference with a pattern that doesn't include a reference?
Here's an example using a struct pattern:
fn main() {
struct S(u32);
let S(x) = &S(2);
// type of x is `&u32`
}
The behavior is…

Max Heiber
- 14,346
- 12
- 59
- 97
5
votes
2 answers
Weird type when pattern matching references
I encountered this strange behaviour when reading this post, and the core question of this post is when you matching (&k, &v) = &(&String, &String) , k and v will get the type String .
To figure out what's happending, I wote the following test code,…

Steve Lau
- 658
- 7
- 13
2
votes
1 answer
What will match ergonomics do when encounters reference pattern?
I have read the rfc 2005, knowing the process of manipulation is a repeated operation. And I say encounters reference pattern, I am not talking about encountering reference pattern at the first iteration like the following one:
let x = &String;
//…

Steve Lau
- 658
- 7
- 13
0
votes
0 answers
Why if let with reference to some yields reference to inner value?
The following code doesn't compile because assigning a to b returns: expected struct std::string::String, found &std::string::String. I thought &s was &Option and not &Option<&T>. It seems that &s is behaving exactly like s.as_ref(). What's the…

Danilo Souza Morães
- 1,481
- 13
- 18