Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the '@' symbol do in Rust?

Tags:

rust

I forgot to specify the type of a parameter and the error message was as follows:

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?

like image 280
Michael Dorst Avatar asked Apr 18 '18 18:04

Michael Dorst


People also ask

What does the & symbol mean in Rust?

An ampersand ( & ) is used with a variable's name while passing its reference instead of its value to a function. The function's signature should also have an ampersand with the type of argument that receives the reference. When a function takes in a reference as a parameter, it is called borrowing.

What is the * operator in Rust?

operator in Rust is used as an error propagation alternative to functions that return Result or Option types.

What is the :: in Rust?

Please review Appendix B: Operators and Symbols of The Rust Programming Language. In this case, the double colon ( :: ) is the path separator. Paths are comprised of crates, modules, and items. The full path for your example item, updated for 1.0 is: std::usize::BITS.

What does * do in Rust?

It dereferences num so you assign to the place in memory it points into (the num variable in main() ) and not to the num variable.


1 Answers

You can use the @ 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) {} 
like image 197
Wesley Wiser Avatar answered Sep 16 '22 14:09

Wesley Wiser