Avatar ·

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.

\n\n

For example, I wrote a simple program that tries to change a variable's value through a function:

\n\n
fn 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\n

The compiler complains about the line *val = 100, saying something about val not being mutable.