On 27/05/2014 18:27, Tommi wrote:
What is the meaning of this 'box ref foo' syntax found in the tutorial over at 
__http://doc.rust-lang.org/tutorial.html#references

(Sorry for uglifying the link, my posts seem to get flagged as spam if they 
contain links)

In short, it's:

enum Shape { Sphere(Box<f32>) }

let shape = Sphere(box 1.0f32);
let r = match shape {
    Sphere(box ref radius) => *radius
};

I thought the 'box' keyword meant: "allocate on heap and wrap into a
Box". That doesn't make sense to me in the context of 'box ref
radius'.

That’s what it means in an expression, but the arms of a 'match' statement start with patterns, not expressions. In a pattern, the 'box' keyword means deconstruct a Box<T> type to access the T inside. The 'ref' keyword (which can be used without 'box') means to take a &T reference rather than move the value.

--
Simon Sapin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to