Depending on what you are trying to do, there are multiple answers to
your (implied) question. In general you are not permitted to have two
borrows of the same element at the same time; in this case, your
program is being rejected because the compiler is conservative and it
assumes that any two array elements might be the same.
However, it appears from your example that the two borrows will not
overlap in time. In that case, you just need to help the compiler out
by creating a scope that indicates how long the first borrow should
last:
> fn f1 (b: &mut ~[int]) {
> let mut a;
> {
> a = &mut b[0];
> //process(a);
> }
>
> {
> a = &mut b[3];
> }
> }
The explicit blocks indicate to the compiler how long the borrowed
value can be used, and based on that it can see that the first
borrow and the second will not overlap.
If you DO want multiple borrows that overlap in time, you can use the
`split_mut()` method.
Niko
On Wed, Aug 21, 2013 at 11:38:49AM +0000, Piyush Agarwal wrote:
> Hi All,
>
> Rust Compiler is giving error in following code :-
>
>
>
> fn main() {
>
> let mut vec = ~[1 ,2 , 3, 4];
>
> f1(&mut vec);
>
> }
>
> fn f1 (b: &mut ~[int]) {
>
> let mut a = &mut b[0];
> //process(a);
>
> a = &mut b[3];
> }
>
>
>
> error: cannot borrow '(**b)[]' as mutable more than once at a time
>
>
>
> Thanks and Regards,
>
> Piyush Agarwal
>
>
>
> [cid]
>
> *
> _______________________________________________
> 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