The view method provides an *immutable view*, which is why you're getting a compilation error. I suggest the function `vec::mut_view()`---unfortunately it does not appear to be offered as a method at the moment. We're still much too inconsistent about that, but methods are the future. So just change `let part1 = v.view(0, 5)` to `let part1 = vec::mut_view(v, 0, 5)` and you should be able to modify `part1[2]`.

The `vec::slice()` function copies data out and results in a new, unique array, so changes to the result of `vec::slice()` will not affect the original. However, the current `slice()` is deprecated and the plan is to rename what is now `view()` to `slice()` once `slice()` is removed (see issue #3869 [1]). Sorry for the confusion, pardon our dust and all that. (The current `slice()` function predates the existence of what we now call slices)

You cannot send slices (that is, the result of view()) over a channel. A slice is a borrowed vector---and you can never send borrowed content over channels, only owned content. You can invoke `slice.to_vec()` to copy out the data from the slice into a fresh, owned vector, and then send that.

Hopefully that helps!


regards,

Niko

[1] https://github.com/mozilla/rust/issues/3869

Paul Harvey wrote:
Hi,

So, i am trying to get my head around slices in Rust, specifically how
being unique affects them.

In C i can do the following, and when i am done fiddling, the values
are in the struct.

struct foo{
int a[100];
int b[100];
  }

  struct foo f;
  int dumb_a =&f.a;
  int dumb_b =&f.b;


fiddle(dumb_a);
fiddle(dumb_b);


I am trying to figure out if this is possible in Rust with unique types:

fn main(){
let mut v = ~[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,];

let mut part1 = v.view(0,5);
let mut part2 = vec::slice(v, 6 , 10);
let mut count = 0;
for 5.times{
io::println(fmt!("%?", part1[count]));
count = count + 1;
}
part1[2] = 3;
}


This code is giving me an error, and i am not sure how to devlace a
unique vector with mutable content.

Now apart from the fact that i am getting a compile error, would the
values of vector v be changed after the statement : part1[2] = 3; ????

Is this even legal?

What would happen if i sent my slice over a channel and fiddled with it?

Is that allowed?

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

Reply via email to