error[E0603]: function, struct or module is private
You used a function, struct, constant or module that is private to the module it lives in. Everything in a Rust module is private unless it’s marked pub.
Quick fix: Add pub to the item (and to any modules on the path to it).
Code that triggers E0603
mod garden {
fn plant(seed: &str) {
println!("planting {seed}");
}
}
fn main() {
garden::plant("tomato");
}What the compiler says
error[E0603]: function `plant` is private
--> src/main.rs:8:13
|
8 | garden::plant("tomato");
| ^^^^^ private function
|
note: the function `plant` is defined here
--> src/main.rs:2:5
|
2 | fn plant(seed: &str) {
| ^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0603`.Why Rust raises E0603
Privacy in Rust works per module. An item without pub is visible inside its own module and that module’s children, but not to the code around it. plant sits in mod garden without pub, so main can’t call it. The note shows exactly where it’s defined.
Every module on the path needs to be visible too. A pub fn inside a private module is still unreachable from outside its parent.
Structs have one more rule: pub struct makes the type public, but each field stays private until you mark it pub as well. Reading a private field is error E0616, and setting one in a struct literal is E0451.
How to fix E0603
1. Mark the item pub
Make it part of the module’s public API. Mark only what callers need: keeping helpers private is the point of modules.
mod garden {
pub fn plant(seed: &str) {
println!("planting {seed}");
}
}
fn main() {
garden::plant("tomato");
}Related errors
- E0433: failed to resolve: use of undeclared type
- E0425: cannot find value in this scope
- E0609: no field on type
Learn the concept behind it
The Packages, Crates and Modules lesson explains the rules behind E0603 with diagrams and more examples. For the official reference, run rustc --explain E0603 or read the E0603 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.