patternrustMajor
What does the '@' symbol do in Rust?
Viewed 0 times
thedoesrustwhatsymbol
Problem
I forgot to specify the type of a parameter and the error message was as follows:
Which raises the question: what can you do with an
error: expected one of : or @, found )
--> src/main.rs:2:12
|
2 | fn func(arg)
| ^ expected one of : or @ here
Which raises the question: what can you do with an
@ symbol? I don't remember reading about using the @ symbol for anything. I also did some Googling and couldn't find anything. What does @ do?Solution
You can use the
Assignments in Rust allow pattern expressions (provided they are complete) and argument lists are no exception. In the specific case of
@ symbol to bind a pattern to a name. As the Rust Reference demonstrates:let x = 1;
match x {
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}Assignments in Rust allow pattern expressions (provided they are complete) and argument lists are no exception. In the specific case of
@, this isn't very useful because you can already name the matched parameter. However, for completeness, here is an example which compiles:enum MyEnum {
TheOnlyCase(u8),
}
fn my_fn(x @ MyEnum::TheOnlyCase(_): MyEnum) {}Code Snippets
let x = 1;
match x {
e @ 1 ... 5 => println!("got a range element {}", e),
_ => println!("anything"),
}enum MyEnum {
TheOnlyCase(u8),
}
fn my_fn(x @ MyEnum::TheOnlyCase(_): MyEnum) {}Context
Stack Overflow Q#49906483, score: 52
Revisions (0)
No revisions yet.