The println!
macro handles both values and references without requiring explicit dereferencing.
First, create a vector
let v = vec![0, 2, 3, -4];
Printing references from
vec.iter
for x in v.iter() { println!("x: {}", x); }
Printing dereferenced elements from
vec.iter
for x in v.iter() { println!("x: {}", *x); }
Printing values from
vec
for x in v { println!("x: {}", x); }
How is the internal dereferencing in Case 1 done?
I know internally println!
makes another macro call but the last macro in the chain format_args!
is implemented at the compiler level and I have no view into it.
macro_rules! println {
($fmt:expr) => (print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
}
macro_rules! print {
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
}
macro_rules! format_args {
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
}