Warning
This post was published 46 days ago. The information described in this article may have changed.
I've some of the concepts of references, and that it solves multiple living-refences that would otherwise lead to double free, or to access de-allocated data.
In the case below, one isn't using references but just "following the pointers" with *
. I'm aware that "pointer" isn't the best concept maybe, but idk what to use here.
I think the reason why the first one is not allowed is what I stated above (but may be wrong?)
However, I don't get why wouldn't the Box<T>
behave the same. Instead it moves the data, or copies it if it's a number.
Is there a simple explanation? (up to beginning of chapter 5 this wasn't yet explained in the book, imho.)
fn main(){
dereferencing_cases();
}
fn follow_the_pointer_cases() {
let v = vec![String::from("meow")];
let y = *v[0]; // Does not or it could cause troubles (maybe double frees)
// or access data that has moved i.e v2[0]
let ss = Box::new(String::from("My String"));
let ss2 = *ss; // works fine, and moves the String!
println!("{ss2}")
// so println!("{ss:?}") would fail
}
(tbc: i'm aware we can just clone it as an option.)
6 posts - 5 participants
🏷️ rust_feed