vbezhenar
vbezhenar

Reputation: 12316

Cannot modify a struct field from implementation: "cannot borrow immutable borrowed content as mutable"

I'm trying to implement an iterator which will yield prime numbers. I store already found prime numbers in a Vec<u64>.

Here's my implementation:

struct Primes {
    primes: Vec<u64>,
}

impl Primes {
    fn new() -> Primes {
        Primes { primes: vec!(2, 3) }
    }

    fn iter(&self) -> PrimesIterator {
        PrimesIterator { primes: &self.primes, index : 0 }
    }
}

struct PrimesIterator<'a> {
    primes: & 'a Vec<u64>,
    index: usize,
}

impl<'a> Iterator for PrimesIterator<'a> {
    type Item = u64;

    fn next(&mut self) -> Option<u64> {
        if self.index < self.primes.len() {
            let result = self.primes[self.index];
            self.index += 1;
            Some(result)
        } else {
            let mut n = *self.primes.last().unwrap();
            loop {
                n += 2;
                if is_prime(self.primes, n) {
                    self.primes.push(n);
                    self.index += 1;
                    return Some(n);
                }
            }
        }
    }
}

fn is_prime(primes: &[u64], n: u64) -> bool {
    for &p in primes.iter() {
        if n % p == 0 {
            return false;
        }
        if p * p > n {
            return true;
        }
    }
    return false;
}

but when I'm trying to compile it, I'm getting the following error:

main.rs: error: cannot borrow immutable borrowed content `*self.primes` as mutable
main.rs:                     self.primes.push(n);

I declared self as &mut so I don't really understand what's wrong here and how to fix that.

Upvotes: 3

Views: 4815

Answers (1)

oli_obk
oli_obk

Reputation: 31163

Your PrimesIterator type contains a non-mutable reference to a Vec<u64>. You need to declare it as a mutable reference:

struct PrimesIterator<'a> {
    primes: &'a mut Vec<u64>,
    index: usize,
}

This will of course require you to also modify the iter() function to make sure it passes a mutable reference:

impl Primes {
    fn new() -> Primes {
        Primes { primes: vec!(2, 3) }
    }

    fn iter(&mut self) -> PrimesIterator {
        PrimesIterator { primes: &mut self.primes, index : 0 }
    }
}

Upvotes: 7

Related Questions