Skip to content

How to correct "prior loan as mutable" error with subsequent method calls? #5829

Closed
@cdleary

Description

@cdleary

Using rust 0.6 from homebrew. Trying to do two subsequent method calls with a mutable borrowed self -- by contrast, any number of calls to "peek" seem to work okay:

struct Numberizer <'self> {
    numbers: &'self[int],
    current: uint
}

impl <'self> Numberizer <'self> {
    fn peek(&self) -> int {
        self.numbers[self.current]
    }
    fn pop(&mut self) -> int {
        let rv = self.peek();
        self.current += 1;
        rv
    }
}

fn main() {
    let numbers: ~[int] = ~[1, 42, 3, 4, 5];
    let mut o = Numberizer {numbers: numbers, current: 0};
    io::println(fmt!("%d", o.pop())); // note: prior loan as mutable granted here
    io::println(fmt!("%d", o.pop())); // error: loan of mutable local variable as mutable conflicts with prior loan
}

(I may be doing it wrong but I figured making a github issue would at least make the fix searchable. :-)

$ rustc --version
rustc 0.6
host: x86_64-apple-darwin

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions