2 ms·
> "why are you forcing me to waste a bunch of time doing needless bookkeeping when I create and pass around a string?" Rust does not force you to do this. You
by zozbot234 5y ago
> "why are you forcing me to waste a bunch of time doing needless bookkeeping when I create and pass around a string?"
Rust does not force you to do this. You can in fact pass around `String`, `Vec<…>` etc, and Rust will use move semantics to make it reasonably efficient. Of course using &str and &[…] is the proper "zero overhead" solution, but it does require some extra effort.
- tmp538394722 5y agoJust guessing, but speaking from my own experience, the trouble came when interacting with existing APIs, where you can’t choose what to pass around. Some expect String while others expect &str. Once you understand why, it’s easy to intuit which one should be used where, but it was a day 1 speed bump for me.
- nextaccountic 5y agoIf you have a String, passing a &str is free, you just.. put a & before your string. that's because if you expect a &str and pass a &String, the &String auto-derefs to &str. This is called a deref coercion[0] Like, if you have fn f(s: &str), you can do: let x: String = String::from(something); f(&x); You can see this in playground here [1] (click "Run") To explain: both String and &str is essentially a pointer plus metadata, but &String is a double pointer (a pointer to a pointer). Rust knows how to convert double pointers to pointers, using the Deref trait. This implicit conversion is applied only in a handful of places, and only if it doesn't lead to any ambiguity (if f received a generic parameter it wouldn't work for example). If you wanted to convert String to &str explictly, you would need to write &*x, like this (see in the playground here [2]): let x: String = String::from(something); f(&*x); That's because if x has type String, *x has type str (because String impls Deref<Target = str> [3]) and &*x has type &str. This means that auto-deref changed your &x into &*x automatically. [0] https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-d... [1] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5589e478f958429cfbed3d85cb14ab12 https://play.rust-lang.org/?version=stable&mode=debug&editio... [2] https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ebb5da3211e3ac35ed44b39ba780fa58\\ https://play.rust-lang.org/?version=stable&mode=debug&editio...* [3] https://doc.rust-lang.org/alloc/string/struct.String.html#impl-Deref https://doc.rust-lang.org/alloc/string/struct.String.html#im...
- ironmagma 5y agoYou can also... just allow a copy to occur. My friend is always talking my ear off about how people are so afraid to copy a string in Rust, going through insane motions to avoid it, when there is some actual quadratic complexity somewhere else that's actually eating up time, whereas a single string copy is undoubtedly taking mere nanoseconds.
- ben-schaaf 5y agoMy guess would be that it's people (like myself) being burnt by C++'s copy semantics, where you have to deliberately and consciously avoid making copies everywhere.
- ironmagma 5y agoYou have to do that in Rust too. But only if you really want to avoid a copy. Who needs to avoid copying strings? Chances are that is not the slow part of your program.
- seoaeu 5y agoI think it is the opposite in Rust. You have to specifically write `.clone()` which makes people feel like they're doing something wrong, when actually it is just a fast and harmless function call