Hi! I do hope everyone is doing well. Pretty much as the title says, I'm very confused with the
Cow smart pointer usage and how it works and when one should use it.
Citing the documentation, it says:
Cowis a smart pointer providing clone-on-write functionality: it can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required
It kindly provides one of the following examples for its
// Extracts the owned data. // Clones the data if it is not already owned. use std::borrow::Cow; let s = "Hello world!"; let cow = Cow::Borrowed(s); assert_eq!( cow.into_owned(), String::from(s) );
So, from my understanding, it borrows a variable but clones it whenever mutation occurs.
I have the following questions:
clone()in a type?
Cowmight be useful?
My apologies if my concern do not make sense, I'm doing my best to understand smart pointers. I'd deeply appreciate any guidance. Thank you in advance.