Deep Dive: egui/eframe for instant UIs — why Immediate Mode fits system languages, State management with Rust's ownership, Layout & Style, Custom painting, and when Retained Mode is the better choice.
Deep Dive: egui/eframe for instant UIs — why Immediate Mode fits system languages, State management with Rust's ownership, Layout & Style, Custom painting, and when Retained Mode is the better choice.
A crate is the smallest amount of code the Rust compiler considers at once. There are two kinds:
main.rs). Every binary crate
has a main function.lib.rs) that other programs can
import. It has no main function.A package is a bundle of one or more crates plus a Cargo.toml file. A
package can contain:
src/bin/)By default Cargo treats src/main.rs as the binary and src/lib.rs as the
library crate. src/main.rs is the crate root — the module where
everything begins.
The crate root is the start
The crate root is the source file from which the compiler starts and from
which your module tree grows (src/main.rs or src/lib.rs). Everything in your
crate hangs off this root. The compiler begins there and follows the mod
declarations.
Check: What's the difference between a binary and a library crate?
Answer: A binary crate becomes an executable, a library crate an importable library
Binary crates (main.rs) produce an executable with main(). Library crates (lib.rs) are importable libraries without main().
⚠ Common mistake: Adding a
fn main()tosrc/lib.rsonly earns you afunction 'main' is never usedwarning — a library crate has no entry point.cargo runkeeps launching the binary fromsrc/main.rs; your library code gets linked, but never runs as a program.
Takeaways:
Modules let you group code into named units and control privacy. By default everything in a module is private — invisible to outsiders:
mod back_of_house {
fn cook_order() {}
}
fn main() {
// Error: cook_order is private!
// back_of_house::cook_order();
// → error[E0603]: function 'cook_order' is private
}
To access a module's contents you use paths:
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
fn main() {
// Absolute path (with crate::)
crate::front_of_house::hosting::add_to_waitlist();
// Relative path (without crate::)
front_of_house::hosting::add_to_waitlist();
}
pub makes an item public. Important: pub on a module makes the module
visible, but not automatically its contents — each item needs its own pub.
Private by default
In Rust everything is private unless you say pub — the opposite of many
languages. This rule forces you to consciously decide what becomes part of your
public API — a core piece of Rust's "safe by default" philosophy.
Check: What does the pub keyword do on a module?
Answer: It makes the module visible, but not automatically its contents
pub on a module only makes the module public. Its contents stay private until they themselves are set to pub.
⚠ Common mistake:
pub mod hostingalone isn't enough: withoutpubon the function itself you geterror[E0603]: function 'add_to_waitlist' is private.pubdoesn't cascade inward — every item needs its ownpub.
Takeaways:
Instead of writing the full path every time, bring an item into scope with
use:
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
use crate::front_of_house::hosting;
fn main() {
hosting::add_to_waitlist();
// → compiles; without the use statement: error[E0433]: failed to resolve: use of undeclared crate or module `hosting`
}
After that you write hosting::add_to_waitlist() instead of the full path.
use the parent module (not the function) so it's clear
where it comes from — hosting::add_to_waitlist().use the full path — use std::collections::HashMap;
then HashMap::new().use std::io::Result as IoResult;.Why parent module for functions?
For functions you keep the parent module in scope: hosting::add_to_waitlist()
shows the function is defined elsewhere. For structs/enums you bring the full
path — there's no meaningful parent "location". These are conventions, not
rules; Rust accepts both.
Check: How do you conventionally bring a function into scope?
Answer: use the parent module, then module::function()
Convention: functions via their parent module (hosting::add_to_waitlist()) so it's clear it's defined elsewhere. Structs/enums are brought in with the full path.
⚠ Common mistake: Two glob imports like
use a::*;anduse b::*;lead toerror[E0659]: 'Config' is ambiguousas soon as both modules provide an item namedConfigand you use it. Globs don't shadow anything — on a collision you need a concrete import or an alias withas.
Takeaways:
When a module grows, put it in its own file. From
// lib.rs
mod front_of_house {
pub mod hosting { /* ... */ }
}
it becomes:
// lib.rs
mod front_of_house;
// Contents now live in src/front_of_house.rs
Rust then looks for front_of_house's contents in src/front_of_house.rs.
If a module has submodules, use a directory:
// lib.rs
mod front_of_house;
// src/front_of_house/mod.rs
pub mod hosting;
// src/front_of_house/hosting.rs
mod front_of_house; looks first for src/front_of_house.rs, then
src/front_of_house/mod.rs. (The mod.rs style is older; today one prefers
front_of_house.rs next to a front_of_house/ folder.)
The compiler follows mod declarations
The crate root is the only place Cargo knows. From there the compiler follows
mod declarations and loads the matching files. So every file must be reachable
via a chain of mod statements from the root — only files in the module tree
get compiled.
Check: Where does Rust look for the contents of 'mod front_of_house;'?
Answer: In src/front_of_house.rs or src/front_of_house/mod.rs
mod name; makes the compiler look for src/name.rs, or (older) src/name/mod.rs for modules with submodules.
⚠ Common mistake: A new file
src/front_of_house.rsalone never gets compiled — withoutmod front_of_house;in the crate root it stays invisible. Every file must be reachable through a chain ofmoddeclarations from the root, otherwise it simply doesn't exist for the compiler.
Takeaways:
Besides pub (visible to all) and private (module only), there are finer
levels:
pub(crate) — visible within the whole crate, but not from outsidepub(super) — visible in the parent modulepub(in path) — visible within a specific pathmod secret {
pub(crate) fn internal() {} // usable anywhere in the crate
pub(super) fn in_parent() {} // only in the parent module
fn private() {} // only here
}
For a library crate: pub makes an item part of the public API for other
crates. Everything you mark pub is a promise to your users — changing it
breaks their code. Choose your public API deliberately.
Less pub = more freedom
The less you make public, the freer you are to restructure internally later.
pub(crate) is a good compromise: the item is usable anywhere in your crate but
not part of the public API — you can change it without breaking users.
Check: What does pub(crate) mean?
Answer: Visible in the whole crate, but not from outside
pub(crate) makes an item usable anywhere in your own crate but keeps it out of the public API — ideal for internally shared code.
⚠ Common mistake:
pub(crate)applies strictly to the crate it is defined in. Move the code into a separate workspace crate later and access from there fails with anis privateerror — at that point you need realpubitems behind a deliberate API.
Takeaways:
A re-export makes an item available at a different location:
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
}
}
// Re-export: makes add_to_waitlist directly available here
pub use crate::front_of_house::hosting::add_to_waitlist;
fn main() {
add_to_waitlist();
}
Users then write add_to_waitlist() instead of front_of_house::hosting::....
Re-exports are ideal for building a clean public API while the internal
structure stays free.
Free inside, clear outside
The module tree has two roles: internally it helps you organize code;
externally it is your API. With pub use you decouple the two: inside you
structure for maintainability, outside you show a clear, stable interface.
Check: What is a re-export with 'pub use' for?
Answer: Providing an item under a different, user-friendly path
pub use provides an item at a new location — so you offer a clean public API while the internal module structure stays free.
⚠ Common mistake: A bare
use crate::front_of_house::hosting::add_to_waitlist;is not a re-export, just a private import for your module. Users of your library get no access through that path — onlypub usemakes it part of the public API; before that, their import fails.
Takeaways:
You structure a small program following the rules of this module:
kitchen with a public submodule ordering.order(dish: &str) that prints the dish
name.use and call it in main.pub(crate) and another one private — and check that
the private one is unreachable from outside.This connects mod, pub, paths, use, and visibility into a whole.
Recipes from this module
mod name { ... } or mod name; (file)pub, pub(crate), pub(super)use path::to::item;pub use path::to::item;Scenario: The monolith
⚠ Common mistake:
src/main.rsandsrc/lib.rsare two separate crates. Declaremod kitchen;only inlib.rsand then callcrate::kitchen::...inmain.rs, and it fails witherror[E0433]: failed to resolve— the binary reaches the library only through its name, e.g.use my_project::kitchen;.
Takeaways:
Course navigation: ← Module 13 · All 27 modules · Module 15 →