https://www.theregister.com/2023/07/18/linux_desktop_debate/
It would be twice as much if people could stop arguing about who counts and who doesn't
I'm going through the interactive version of The Book, and I'm confused by the results of an exercise in Ch 4.3 - Fixing Ownership Errors.
The following code does not work, and they say it's because it would result in the same heap space being deallocated twice:
fn main() {
let s = String::from("Hello world");
let s_ref = &s; // reference s
let s2 = *s_ref; // dereference s_ref
println!("{s2}");
}
But in my mind, this should be equivalent to the following compilable code, which transfers ownership of s
to s2
:
fn main() {
let s = String::from("Hello world");
let s_ref = &s; // reference s
let s2 = s; // move s directly
println!("{s2}");
}
If s_ref
is a reference to s
, then dereferencing s_ref
should return the String s
, shouldn't it? Why can't s
be moved to s2
with either the above code or let s2 = *&s;
, which fails in the same way?
@b_van_b
@programming.dev