I notice that I constantly need to do String::from("mydummystring") in Rust. Does the call to from() end up creating a full copy of the string data? Seems that would be a major performance issue.
I notice that I constantly need to do String::from("mydummystring") in Rust. Does the call to from() end up creating a full copy of the string data? Seems that would be a major performance issue.
Yes, From<&str> for String works by creating a new heap-allocated buffer and copying the data from the original slice into it.
Seems that would be a major performance issue.
Less than you'd think, probably. Copying a chunk of bytes is usually very fast. You should benchmark your code to know if copying is a bottleneck. Even when copying is slow, most non-trivial programs probably do some copying of strings; you can't just never copy anything.
Nevertheless, if there is a lot of unnecessary copying going on and it is a problem for your design, Rust offers plenty of other options. String is not Rust's only string type, or even the most frequently used one; if you don't need to copy strings, you can often borrow them using &str, share ownership using Rc<str>, defer the owning decision to runtime using Cow<str>, or even use a string interning library such as string-cache. Ownership and copying of values is under your control with Rust, much more so than with most other languages, in fact. Ultimately, it's up to you to choose what kind of ownership makes the most sense for strings in your program.