Skip to content

Cannot move out of dereference for struct with vector. #15495

Closed
@JP-Ellis

Description

@JP-Ellis

I have encountered an issue which seems very unusual to me and I believe it might be a bug.

Below is a small example which illustrates the issue.

struct CountDown {
    count: uint,
    // Including the vector causes `Some(self.current())` to fail later on.
    vector: Vec<uint>
}

impl CountDown {
    fn new() -> CountDown {
        CountDown {
            count: 10,
            vector: vec!(3u, 4, 5)
        }
    }

    fn current(self) -> uint {
        self.count
    }
}

impl Iterator<uint> for CountDown {
    fn next(&mut self) -> Option<uint> {
        self.count -= 1;
        if self.count == 0 {
            None
        } else {
            // Whether the vector is in the struct or not, this work:
            //Some(self.count)

            // When the struct contains a vector, the following causes the error:
            //   cannot move out of dereference of `&mut`-pointer
            // If the vector is not in the struct, this work fine.
            Some(self.current())
        }
    }
}

fn main() {
    for n in CountDown::new() {
        println!("n = {}", n);
    }
}

It appears to me quite strange that the presence of a Vector within the struct (even if it isn't used) causes the issue. It also seems strange to me that returning a struct field works, but returning a method which itself returns that struct field fails.

Admittedly, I don't really understand what cannot move out of dereference of '&mut'-pointer really means; a Google search doesn't yield very much, and the documentation doesn't seem to contain much regarding the error.

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