On 1/24/13 10:51 AM, Peter Ronnquist wrote:
>**>* Hi,*>**>* I am trying to create a vector of pointers into another vector 
>without*>* much luck. I would be happy if someone could tell me if this a good 
>way*>* to use rust or if there is a better way to achieve a similar data*>* 
>structure or if I am using the wrong language :-)*>**>* I have a vector of 
>physical objects:*>**>**>* obj_vec = [ mut @obj1, @obj2, @obj3, ...,  @objn 
>]*>**>* I will iterate through those objects and see which ones are close 
>to*>* each other and I want to store a pointer/reference to those objects in 
>a*>* separate vector:*>**>**>* obj_close_vec = [ &obj2, &obj3 ]*>**>* The 
>objects in obj_close_vec will be investigated further and might be*>* updated.*
Does something like this work?

     let mut obj_vec = [ @mut obj1, @mut obj2, ... @mut objn ];
     let mut obj_close_vec: ~[@mut ObjType] = ~[];
     for obj_vec.each |&obj| {
         if obj.is_interesting() {
             obj_close_vec.push(obj);
         }
     }
     for obj_close_vec.each |&obj| {
         if i_want_to_mutate(obj) {
             *obj = munge(obj);
         }
     }

Patrick
----------------------------------

I tried this:


struct Vec2 {
    x: float,
    y: float
}

fn main () {

    let mut obj_vec = [@mut Vec2 {x: 0.3, y: 0.4}, @mut Vec2 {x: 0.1, y: 0.2} ];

    let mut obj_close_vec: ~[@mut Vec2] = ~[];

    for obj_vec.each  |&obj| {
        obj_close_vec.push(obj);
    }

}

and got:


rustc test_vec.rs

test_vec.rs:14:24: 14:27 error: obsolete syntax: by-mutable-reference mode
test_vec.rs:14     for obj_vec.each  |&obj| {
                                       ^~~
note: Declare an argument of type &mut T instead

error: aborting due to previous error


hmm, don't know what that means but I will try to get past that error.
Thanks for your quick reply!
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to