I need to convert the Vec body from a websocket (tungstenite) response to a String. Because String::from_utf8 takes a value (takes ownership), I needed to call clone on the body:
match connect("wss://...") {
Ok((mut socket, response)) => {
let body = String::from_utf8(response.body().clone().unwrap()).unwrap();
It just feels wrong to have to do this. Is there a better alternative, in this specific case?
self argument, without &, moves the whole object into method's body. Move means the caller loses access to it. Ownership is exclusive, so this guarantees that for this instance of self this variable is the only place where it exists, and there's no other way to access this object from anywhere else in the program.
Because this self has full exclusive access and full ownership, it is allowed to remove the body field, which is also exclusively owned here.
Rest of the self object will be automatically destroyed, because this function had the last and only way to access this object, and it didn't store or return it anywhere (it returned only the body object which will be kept alive)