November 2017
Intermediate to advanced
264 pages
5h 45m
English
Items in a module are by default only visible in the module itself; they are private to the module they are defined in. If you want to make an item callable from code external to the module, you must explicitly indicate this by prefixing the item with pub (which stands for public). In the following code, trying to call func1() is not allowed by the compiler--error: function `func1` is private:
// from Chapter 8/code/modules.rs
mod game1 {
// all of the module's code items go in here
fn func1() {
println!("Am I visible?");
}
pub fn func2() {
println!("You called func2 in game1!");
}
}
fn main() {
// game1::func1(); // <- error!
game1::func2();
}
Calling func2() works without any problem because it is public, and this prints ...
Read now
Unlock full access