Is it possible to make a private variable in Rust?

Viewed 15377

I'm trying to generate prime numbers. The code needs to store all the generated primes (to generate the next), to have some private functions to help and one public function (generate_next_prime).

In Java or C++, I would write a PrimesGen class, but in Rust there can't be private variables in a struct. In Python I would probably write a PrimesGen module, but in Rust modules can't have variables.

This code compiles and runs:

struct PrimesGen {
    primes_so_far: Vec<i32>,
    next_candidate: i32,
}

impl PrimesGen {
    pub fn new() -> PrimesGen {
        PrimesGen {
            primes_so_far: vec![],
            next_candidate: 2,
        }
    }
}

fn main() {
    let pg: PrimesGen = PrimesGen::new();
    println!("{}", pg.next_candidate);
}

So what do I do?

1 Answers
Related