0
fn main() {
    let mut v = vec![100, 32, 57];

    for i in &mut v {
        println!("{}", type_of(i));
    }
}

fn type_of<T>(_: T) -> String {
    let a = std::any::type_name::<T>();
    return a.to_string();
}

>> &mut i32
>> &mut i32
>> &mut i32

I can understand this.

fn main() {
    let mut v = vec![100, 32, 57];

    for &mut i in &mut v {
        println!("{}", type_of(i));
    }
}

fn type_of<T>(_: T) -> String {
    let a = std::any::type_name::<T>();
    return a.to_string();
}

>> i32
>> i32
>> i32

Why this is showing "i32" ????

It seems like trying "&mut &mut i32" but I cant understand why that "&mut" reference resolved in this case...

I thought this is doing same thing under code.

let mut a = 5;
let mut b = &mut a;
println!("{}", type_of(&mut b));

>> &mut &mut i32
>> &mut &mut i32
>> &mut &mut i32
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
a a
  • 1

0 Answers0