4 ms·
Nice writeup. I'd like to see some examples illustrating appropriate usage of Clone-on-Write with Rust strings - that's something I personally have struggled w
by dallbee 11y ago
Nice writeup.
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.
- steveklabnik 11y agoCow 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.html#method.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.
- llogiq 11y agoI blogged about that some time ago: https://llogiq.github.io/2015/07/09/cow.html https://llogiq.github.io/2015/07/09/cow.html
- tatterdemalion 11y agoI'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.
- deleted 11y ago[deleted]