"box" and placement "in"


#1

I just read through the box/placement-in RFC (https://github.com/rust-lang/rfcs/blob/master/text/0809-box-and-in-for-stdlib.md), and I have to admit I am a bit confused. In particular, I do not understand the distinction between these two operations. “box” is for placing stuff into Box/Rc/…, and “in … { … }” is for more generally placing things anywhere? Also if I understand correctly, “box <expr>” cannot be assigned a type, so I have to do things like "let x:Rc <_> = box ".

Hence I wonder why the box syntax even exists, instead of using something like “in Rc::new() { … }”: “Create a new Rc, and initialize something in there”. This seems pretty symmetric to the classical placement-in example, “append an element to the vector and initialize something in there”. This style of writing “box” also clearly defines the type - so while it is more verbose than the existing one (of course, the syntax could be tuned), it requires less verbosity on the other side of the assignment. All in all, it looks to me like a unification of the two constructs.

Now, I assume going with one instead of two constructs has been discussed and there’s a reason not to do it, but I couldn’t find that discussion anywhere. I would appreciate a pointer.


#2

In many cases box can have its type inferred. e.g.

let node = box Node::new();
parent.left = Some(node);

Will correctly infer that only Box<Node> is the correct type. Whereas e.g. for emplace_back functionality there is no way to infer that the operation should insert into a Vec. So box is basically making this particular idiom a bit more pleasant using return-type inference/overloading.


#3

I see. Thank you for the quick reply!