3 ms·
In C++ this is a pretty common thing due to the fact that `const char *` is pretty annoying to work with, so people often use heap-allocated `std::string` inste
by pcwalton 13y ago
In C++ this is a pretty common thing due to the fact that `const char *` is pretty annoying to work with, so people often use heap-allocated `std::string` instead. In Rust `&'static str` and `StrBuf` are on more equal footing, so it's not necessary to place objects on the heap to make it easier to work with them. Still, some people do it accidentally, so we've recently made some changes to make it harder to shoot yourself in the foot in terms of performance, most notably removing the `~"foo"` syntax for heap allocated string literals (which also had the side effect of making the language easier to read).
- acqq 13y agoYes, I remember now I've seen a lot of return ~"something" lines. Can you please point me to some complete-enough example of the new "lighter" string return? Thanks. Edit: I guess the key is in 'if you must return the string on the heap.' Why should be common that anybody must return the string on the heap? Why shouldn't be possible to program so that only the receiver must place something on the heap due to his own needs? Why shouldn't the one doing return be able to always return "something"? Couldn't it be made possible in the language? The one doing return shouldn't need to put something he knows it's static on the heap, especially as most probably the receiver will just discard it as soon as it reads the content.
- pcwalton 13y agoJust use `return "something"`. That returns a `&'static str`. If you must return a heap allocated string, the you can use `return StrBuf::new("something")`.
- acqq 13y agoWhy is there a "must" for returning the string on the heap? Can't it be solved in the language to use the static string up to the point the reallocation is needed?
- ben0x539 13y agoRust has builtin types &'static str for strings that live for the entire runtime of the program and ~str for strings that live in dynamically managed heap memory (from the more general ~T for T that live there with T=str). If the function always returns a static string it can return the former type and won't need a ~ or any allocations for that. If you need something more dynamic... you could have a user-defined type that can hold either and remembers whether it's responsible for freeing the memory, but that doesn't fall out of the builtin types naturally.
- heinrich5991 13y agoThere is MaybeOwned for that. It's either a &'static str or a ~str IIRC.
- pcwalton 13y agoIt would require runtime overhead over C/C++ to check whether every pointer is really is in static or heap memory every time you want to free it.
- acqq 13y agoAs far as I understand, the effective overhead of "free" can be practically zero if the compile-time construction of the static strings would contain some "nothing to deallocate here" info versus any info which anyway has to be run-time maintained to anything that is really allocated. Any non-trivial allocator has a bunch of checks to do (e.g. different-sized small objects are typically allocated in separate blocks, differently to the big allocations etc). As soon as your language doesn't use C-like zero-terminated characters but anything with more information, it's trivial to avoid these unnecessary copy-to-heap-just-to-use-and-deallocate steps.
- dbaupp 13y agoAs other have said: it can be solved in the library by defining a string type with the appropriate semantics (e.g. the MaybeOwned[1] type they refer to). [1]: http://static.rust-lang.org/doc/master/std/str/enum.MaybeOwned.html http://static.rust-lang.org/doc/master/std/str/enum.MaybeOwn...
- nly 13y agoBoost has 'string_ref'[0] for this. Something similar is currently a standard proposal under the name 'string_view'[1] [0] http://www.boost.org/doc/libs/1_55_0/libs/utility/doc/html/string_ref.html http://www.boost.org/doc/libs/1_55_0/libs/utility/doc/html/s... [1] https://isocpp.org/files/papers/N3921.html https://isocpp.org/files/papers/N3921.html