In Rust, a file is an implicit module. When you put some code in the foo.rs file, if you want to use this code, you must enter mod foo; , because the name of this file is implicitly the name of the module. The file with the main one is no exception: it is one module (basic module).
Now, inside the module, everything can have access to everything. Take a look at this small example to see:
struct Foo { x: i32,
Bar can access private members of Foo : in Rust, visibility works with a module, not a struct. Inside the same module you cannot do something personal for the same module.
So, if you want to make the variable private in your example, put your structure and implementation inside the module:
mod prime { pub 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() { use prime::*; let pg: PrimesGen = PrimesGen::new(); println!("{}", pg.next_candidate);
Boiethios
source share