Re:String types in Rust
by steveklabnik on Monday March 28, 2016 @05:04PM
(#11377390)
Cow in general is on my shortlist for "I need to improve the docs."
One area where it's used effectively in the stdlib is String::from_utf8_lossy: http://doc.rust-lang.org/collections/string/struct.String.ht...
If the argument is valid UTF-8, then no allocations need to be performed, and so the returned value will be a &str. But if it's not valid UTF-8, the replacement characters need to be inserted, which means allocation, which means String will be returned.
I blogged about that some time ago: https://llogiq.github.io/2015/07/09/cow.html
Re:String types in Rust
by tatterdemalion on Monday March 28, 2016 @05:42PM
(#11377650)
I've used it when I wanted to provide a value which was statically initialized with a literal in the source, but which could possibly mutate during program execution. Instead of doing `String::from` and performing the heap allocation regardless, the heap allocation is only performed if you actually mutate it.
I'd like to see some examples illustrating appropriate usage of Clone-on-Write with Rust strings - that's something I personally have struggled with while learning rust.