On 02/13/2013 04:32 PM, Nikolas Everett wrote:
I found rust last night and am pretty impressed by what I've read so far but I have a question. In the tutorial you seem to switch between pointer types quite a bit when I don't _think_ it is relevant. Here is an example:
fn  draw_all<T:Drawable>(shapes:~[T]) {
     for  shapes.each  |shape| {shape.draw(); }
}
becomes
fn  draw_all(shapes: &[@Drawable]) {
     for  shapes.each  |shape| {shape.draw(); }
}
Is it relevant that shapes is owned above and borrowed below? If I read the section above right then it is best to declare everything as borrowed unless you need to do otherwise.


It is not relevant. Here @Drawable is a managed object (a boxed type cast to a trait), and at the time the tutorial was written @Object was the only form of this that worked. Even now ~Drawable does not work. This example is as written because of language deficiencies and if I were to redo these examples (and the compiler worked as expected) I would use the same sigil in both.

Also, now that I think about it, is it possible to define shapes as &[&Drawable] and then call draw_all with a &[@Drawable]? I'm trying to wrap my head around all this but intuitively I think of pointers as downgrade-able. Is that right?

No. The conversion from @ and ~ to & is a specific type of coercion called 'borrowing' and it applies to the 'outer' pointer. Treating &[@Drawable] as &[&Drawable] would require more complex subtyping, of which there is very little in Rust.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to