Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Ch::15::Box:: I find this example-selection a bit confusing
Ch::15::Box:: I find this example-selection a bit confusing
โ rust ๐ 2025-07-03 ๐ค surdeus ๐๏ธ 3Here the point of the article and the write up is very good, and very clear.
But there is an important caveat imho, in this paragraph:
The reason the
deref
method returns a reference to a value, and that the plain dereference outside the parentheses in*(y.deref())
is still necessary, has to do with the ownership system. If thederef
method returned the value directly instead of a reference to the value, the value would be moved out ofself
. We donโt want to take ownership of the inner value insideMyBox<T>
in this case or in most cases where we use the dereference operator.
However, Box
seems one of the only cases that this actually happens!
fn main() {
let a = Box::<String>::new(String::from("hell"));
let b = *a; // expects &str
let c = *a; // but clearly wasn't
}
6 posts - 3 participants
๐ท๏ธ rust_feed