3

While trying to answer on Rust array of functions (it's been answered with a great answer), I've coined the following code:

fn main() {
    let mut a : Vec<proc() -> uint>;
    for i in range(0u, 11) {
        a[i] = proc(){i};
    }
    println!("{} {} {}", a[1](), a[5](), a[9]());
}

Please ignore the fact that proc is being deprecated, I just figured out that this is what should be used instead of closures (I didn't know about move and unboxed closures at that time).

However, I was not able to call elements of a vector due the following:

 <anon>:6:26: 6:30 error: cannot move out of dereference (dereference is implicit, due to indexing)
 <anon>:6     println!("{} {} {}", a[1](), a[5](), a[9]());

What this error means? Shouldn't it just return uint?

Community
  • 1
  • 1
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91

1 Answers1

3

proc() were closures that could only be used once, and thus calling them consumed them.

In your situation, this would imply moving the closure out of the Vec<> in order to consume it, which is not possible as indexing is the dereferencing of a & pointer, which only allow immutable access.

Levans
  • 14,196
  • 3
  • 49
  • 53