What do the ampersand (&) and asterisk (*) symbols mean in Rust?
Hello everyone! I just started learning Rust and I can't figure out two operators: & (ampersand) and * (asterisk). In other languages, like C++, I'm used to & being the address-of operator and * being the dereference operator for pointers. But in Rust, it's somehow different.
For example, I wrote a simple program that tries to change a variable's value through a function:
\n\nfn main() {\n let x = 42;\n let y = &x;\n println!("{}", y); // Works, but what's happening here?\n \n let mut z = 10;\n change_value(&z);\n println!("{}", z);\n}\n\nfn change_value(val: &i32) {\n *val = 100; // Error! Why?\n}\n\nThe compiler complains about the line *val = 100, saying something about val not being mutable.
No answers yet.
Log in to leave an answer
Blogs