Pointers in Rust: a guide
words.steveklabnik.com
Pointers in Rust: a guide
1–10 of 33 posts
Re: Pointers in Rust: a guide
#2But pointers in Rust seem pretty well thought out and more sophisticated than what's possible in C++. I'm particularly excited about the compiler error when a borrowed pointer goes out of scope.
Re: Pointers in Rust: a guide
#3These guidelines pretty much apply to C++, although pointers in Rust are more akin to smart pointers than raw pointers, and there are indeed a bunch of use cases for those. Sticking to values and references works pretty well most of the time though. But pointers in Rust seem pretty well thought out and more sophisticated than what's possible in C++. I'm particularly excited about the compiler error when a borrowed po…
Basically, imagine boost's pointer types being in the language, so that the compiler can reason about them.
Re: Pointers in Rust: a guide
#4These guidelines pretty much apply to C++, although pointers in Rust are more akin to smart pointers than raw pointers, and there are indeed a bunch of use cases for those. Sticking to values and references works pretty well most of the time though. But pointers in Rust seem pretty well thought out and more sophisticated than what's possible in C++. I'm particularly excited about the compiler error when a borrowed po…
Yup! If you use C++, this wiki page can help: https://github.com/mozilla/rust/wiki/Rust-for-CXX-programmer... Basically, imagine boost's pointer types being in the language, so that the compiler can reason about them.
Re: Pointers in Rust: a guide
#5There's some good pointers about pointers there. ;)
Re: Pointers in Rust: a guide
#6Earlier quoted context omitted.
Yup! If you use C++, this wiki page can help: https://github.com/mozilla/rust/wiki/Rust-for-CXX-programmer... Basically, imagine boost's pointer types being in the language, so that the compiler can reason about them.
I love it. I'm staring to look for reasons to try Rust in a project, luckily they're piling up :)
Re: Pointers in Rust: a guide
#7However, Steve, I wish you hadn't mentioned managed pointers at all. :P Overuse of managed pointers is another one of those trends that we've noticed in new users, and it's our own fault for making them so deceptively easy to use (at first, anyway... you pay the cost later by making nearly everything else in your code more difficult). We're moving managed pointers out of the language and into the stdlib specifically to discourage this overuse (which will hopefully also embolden users to consider our reference-counted stdlib pointer as an alternative to the GC'd pointer).
Likewise, I don't think that it's important to mention traits in the discussion on owned pointers. Using trait objects is almost always less preferable than just using generics, as trait objects are less efficient than generics and are much more restricted. Not that they don't have their uses, but those uses are few.
In general this is great! We need more articles like this to combat the "oh rust is so complicated it has 200 DIFFERENT TYPES OF POINTERS" propaganda. :)
Re: Pointers in Rust: a guide
#8I'm glad that this guide emphasizes avoiding pointers by default. One of the emerging trends that we've noticed for new users is to allocate on the heap far too often when they could be allocating on the stack instead. I'm also glad that this emphasizes the return-value optimization that Rust uses to make returning a pointer unnecessary. However, Steve, I wish you hadn't mentioned managed pointers at all. :P Overuse…
I originally wrote this as an official guide, so I needed to talk about them, and then I got lazy, and just posted it after the PR got closed. But yes, discouragement is the right approach.
> We need more articles like this to combat the "oh rust is so complicated it has 200 DIFFERENT TYPES OF POINTERS" propaganda.
Exactly. The problem is that pointers are one of the more neat features of Rust, and most people who are new to programming languages like unique[1] features, so it got talked about a lot, so people assumed you needed to know about all these details, when that's not actually true.
I know we all over-used `~str` for a long time...
1: ;)
Re: Pointers in Rust: a guide
#9I'm glad that this guide emphasizes avoiding pointers by default. One of the emerging trends that we've noticed for new users is to allocate on the heap far too often when they could be allocating on the stack instead. I'm also glad that this emphasizes the return-value optimization that Rust uses to make returning a pointer unnecessary. However, Steve, I wish you hadn't mentioned managed pointers at all. :P Overuse…
> I wish you hadn't mentioned managed pointers at all I originally wrote this as an official guide, so I needed to talk about them, and then I got lazy, and just posted it after the PR got closed. But yes, discouragement is the right approach. > We need more articles like this to combat the "oh rust is so complicated it has 200 DIFFERENT TYPES OF POINTERS" propaganda. Exactly. The problem is that pointers are one of…
Kids these days, with your fixed-size arrays and your static strings and your global variables and your hoity-toity region analyses.
Re: Pointers in Rust: a guide
#10These guidelines pretty much apply to C++, although pointers in Rust are more akin to smart pointers than raw pointers, and there are indeed a bunch of use cases for those. Sticking to values and references works pretty well most of the time though. But pointers in Rust seem pretty well thought out and more sophisticated than what's possible in C++. I'm particularly excited about the compiler error when a borrowed po…
Yep, C suffers from pointer abuse and with it, infects any language that uses C compatibility as language subset.