The need for dereferencing

Has anyone else ever questioned the need for a dereferencing operator? Wouldn’t it make more sense the other way around, so we dereference on default and access or modify the reference with a set-pointer-to-operator?

Obviously, println! is able to deference automatically when I’m trying to print a reference of an integer like in the following example. I know that println! is a macro and some Show trait is implemented for this to work but nobody seems to have a problem with that (apart from me because I think its confusing). Why not use this everywhere else?

let mut x = 5;
let y = &mut x;
println!("y: {}", y);
*y += 10;
println!("y: {}", y);

Any thoughts on that?

Currently we have the auto-dereferencing . operator and RFC 241: deref coercions, which means much “deep dereferencing” are not needed.

Also I believe the linked RFC and related RFCs 226, 248 have information on the pros and cons of auto-derefs.

Actually, I don’t think println! is doing auto-derefs, it is more likely that the transitive implementations of Display on references are doing dereferencing here. (Note: Show is being deprecated and replaced by Debug and it corresponds to the {:?} format specifier, not {}.)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.