20 ms·
Wow. Coming from a C++ background, why is it necessary to first dereference, then reference again? That is, `&foo`? And why is triple dereferencing needed? Ther
by marton78 8y ago
Wow. Coming from a C++ background, why is it necessary to first dereference, then reference again? That is, `&foo`? And why is triple dereferencing needed? There are some `&foo`. I thought I had read somewhere that Rust does auto-ref/auto-deref...
- steveklabnik 8y ago> why is it necessary to first dereference, then reference again? This is a conversion; * calls the Deref trait. This is used by smart pointers to return the type inside the smart pointer, so for example, Box<T> is kinda like uniq_ptr<T>. So if foo is a Box<T>, then * foo is a T, and & * foo is a &T. Does that make sense? > And why is triple dereferencing needed? This can happen in generic contexts, sometimes, you end up with a &&String, so if foo is one of those, * foo is a &String, * * foo is a String, * * * foo is a str, and & * * * foo is a &str. (HN formatting does not like this, haha) That's what's happening here. > I thought I had read somewhere that Rust does auto-ref/auto-deref... Only for method calls, that is, foo.bar() will attempt to auto ref/deref foo.