I am new to rust. Learning by coding simple programs. Wrote this simple program:
#[allow(non_camel_case_types)]
fn type_of(_: &T) {
println!("{}", std::any::type_name::())
}
fn main() {
let x = 7;
let y = &x;
let z = &y;
type_of(&x);
type_of(&y);
type_of(&z);
println!("1> x = {}, y = {}, z = {}", x, y, z);
println!("2> x = {}, *y = {},**z = {}", x, *y, **z);
println!("3> x*y = {}", x*y);
// println!("3> y*z = {}", y*z);
}
➜ src git:(main) ✗ cargo check
Finished dev [unoptimized + debuginfo] target(s) in 0.03s
➜ src git:(main) ✗ cargo run
Compiling bits v0.1.0 (/Users/ashokchippa/rust-sandbox/bits)
Finished dev [unoptimized + debuginfo] target(s) in 0.78s
Running /Users/ashokchippa/rust-sandbox/bits/target/debug/bits
i32
&i32
&&i32
1> x = 7, y = 7, z = 7
2> x = 7, y = 7,**z = 7
3> xy = 49
➜ src git:(main) ✗
I'm confused here. Why does rust think 'y' that references x (should be a pointer) has the value same as 'x'? If I take the comment on the last line, I get the following:
➜ src git:(main) ✗ cargo run
Compiling bits v0.1.0 (/Users/ashokchippa/rust-sandbox/bits)
error[E0277]: cannot multiply &{integer}
by &&{integer}
--> src/main.rs:19:30
|
19 | println!("3> yz = {}", yz);
| ^ no implementation for &{integer} * &&{integer}
|
= help: the trait Mul<&&{integer}>
is not implemented for &{integer}
For more information about this error, try rustc --explain E0277
.
error: could not compile bits
due to previous error
which makes sense. So am I to understand that {integer} * &{integer} has an implementation that treats &{integer} the same as *&{integer}? Confusing... Thought this was real basic. What am I missing?