Why do I need to use crate:: to access modules in Rust?
⚓ Rust 📅 2026-04-04 👤 surdeus 👁️ 2I'm reorganising my Rust project into modules, and I have a question about how module paths work. When I structure my project so that each module is located in a different directory, I usually add a mod.rs file in that directory to declare the modules it contains.
However, when I create several modules and need to use one of them from another location, I end up having to write use statements with long or complex paths, often starting with crate::. This makes me wonder why this is necessary and whether there is a cleaner or more idiomatic way to handle it.
Here is an example of my project structure:
src/
├── main.rs
├── models/
│ ├── mod.rs
│ ├── model_task.rs
└── util/
├── mod.rs
└── enum_task.rs
use crate::util::enum_task::StatusTaks;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Todo {
pub id: usize,
pub description: String,
pub status: StatusTaks,
pub create_at: DateTime<Utc>,
pub update_at: DateTime<Utc>,
}
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum StatusTaks {
Todo,
InProgress,
Done,
}
My question is: why do I need to use crate:: to access modules from other parts of my project? Is this the idiomatic way in Rust, or are there better practices to avoid long and repetitive paths?
2 posts - 2 participants
🏷️ Rust_feed