4 ms·
I'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
by kibwen 13y ago
I'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 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. :)
- steveklabnik 13y ago> 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 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: ;)
- kibwen 13y ago~str used to be the only string type we had! And vectors were all on the heap and dynamically-sized!! Kids these days, with your fixed-size arrays and your static strings and your global variables and your hoity-toity region analyses.
- cpeterso 13y agoI'm not sure why people think Rust pointers are so confusing. Any C++ developer familiar with shared_ptr and unique_ptr should understood Rust pointers almost immediately. The area of Rust that I find most confusing (and sends me back to the documentation and example code every time) is the module system and the relationship between library crates, modules, files, and identifier paths. :\
- saosebastiao 13y agoI'm still pretty unexperienced with the language so take this with a grain of salt, but I think one of the reasons people allocate on the heap is because it is so tempting to write functions that accept borrowed pointers as arguments, due to the flexibility of taking either borrowed or managed pointers. However, when you want to call the same function using a stack variable, you have to change your call invocation or the function, which is just an extra pain whenever you want to refactor. For example: fn foo(a: &int){ a.do_something() } fn main(){ let a = @5; let b = ~5; let c = 5; foo(a); foo(b); foo(c); // ack! //laboriously hunt down all my invocations foo(&c); //much better }
- kibwen 13y agoFirst I just want to point out that you're not allocating on the heap just by using references. This: let x = &1; ...is just sugar for: let x1 = 1; // allocated on the stack let x = &x1; // reference to a stack allocation Currently you only allocate on the heap when you use either ~ or @ pointers. And if you're writing an API for widespread consumption, then writing functions that take arguments by reference is actually encouraged since it gives the caller the maximum amount of flexibility. As for the pain of refactoring your code to move from heap-allocations to stack-allocations, this is exactly why we're so fervent to encourage people to use stack-allocated values from the get-go. :) We're fans of Python's philosophy of making things explicit, which explains why we're different from C++ in this particular detail (i.e. we make you explicitly acknowledge when you're passing a reference to a function that expects a reference).
- ww520 13y agoManaged pointers are fine. The only problem I see is ~str and ~[] are managed pointers most of the time and that makes the code looks weird to people. If we make str default to managed rather than ~str, and [] default to managed rather than ~[], most of the weird pointer complaints will go away. Edit: Darn, what was I thinking? I meant owned pointer. Strings or vectors are owned in most cases. That makes Rust code littered with ~str or ~[], especially when having vector of strings or vectors of vectors. ~[~[~str]]. If we make str and [] default to owned, the code would be much cleaner. That means move the syntax for fixed vec to something else, like ![1,2,3]