title: Is it possible to make a private variable in Rust?
body: <p>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 (<code>generate_next_prime</code>).</p>
  
  <p>In Java or C++, I would write a <code>PrimesGen</code> class, but in Rust there can't be private variables in a struct. In Python I would probably write a <code>PrimesGen</code> module, but in Rust modules can't have variables.</p>
  
  <p>This code compiles and runs:</p>
  
  <pre><code>struct PrimesGen {
      primes_so_far: Vec&lt;i32&gt;,
      next_candidate: i32,
  }
  
  impl PrimesGen {
      pub fn new() -&gt; PrimesGen {
          PrimesGen {
              primes_so_far: vec![],
              next_candidate: 2,
          }
      }
  }
  
  fn main() {
      let pg: PrimesGen = PrimesGen::new();
      println!("{}", pg.next_candidate);
  }
  </code></pre>
  
  <p>So what do I do?</p>