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.

I want to avoid making copies of the objects. It would be nice to be able
to allocate the obj_vec once (prefarably on the stack) and then manipulate
the objects through pointers/references.

Stack:
obj_vec                obj_close_vec
[                          [
obj1            |------  &obj2
                  |
obj2 <---------|    |-- &obj3
                       |
obj3 <--------------|   ]

...

objn
]


A small test program:

fn main() {

   let x = @ mut 10;
   let a = x;
   *x = 3;

   // Gives a=3, x=3 as I want.
   io::println(fmt!("a: %d", *a) );
   io::println(fmt!("x: %d", *x) );



   // Same thing with vector elementsx
   let mut v =  @ mut [mut @1, @2];

   let e = v[0];
   // Does not work:
   // error: assigning to dereference of immutable @ pointer
   //   *(v[0]) = 0;

   io::println(fmt!("e: %d", *e) );
   io::println(fmt!("x: %d", *v[0]) );


}

I have struggled with this (different variations of @ ~ vectors and dvec)
for far to long and would appreciate any comments.

Regards
Peter
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to