C and Rust, side by side
static keeps a C function file-private, and the header and Makefile dependency lists are maintained by hand.#include <stdio.h>
/* geometry.h would declare: int area(int w, int h);
Every function below that is not `static` is visible at link time. */
static int clamp_positive(int v) { /* internal linkage: this file only */
return v < 0 ? 0 : v;
}
int area(int w, int h) { /* external linkage: exported by default */
return clamp_positive(w) * clamp_positive(h);
}
int main(void) {
printf("%d %d\n", area(3, 4), area(-2, 5));
return 0;
}
/* Makefile:
app: main.o geometry.o
cc -o app main.o geometry.o
geometry.o: geometry.c geometry.h # forget the header here: a stale build */
src/geometry.rs: clamp_positive is private to the module, and only pub fn area is visible to main.// In a Cargo package this module body lives in src/geometry.rs,
// and `mod geometry;` in src/main.rs or src/lib.rs loads it.
mod geometry {
fn clamp_positive(v: i32) -> i32 { // private by default: this module only
v.max(0)
}
pub fn area(w: i32, h: i32) -> i32 { // `pub` is the export
clamp_positive(w) * clamp_positive(h)
}
}
fn main() {
println!("{} {}", geometry::area(3, 4), geometry::area(-2, 5));
}
| C | Rust |
|---|---|
Functions are exported unless declared static. | Items are private unless marked pub; a call to a private item is E0603. |
| Headers repeat declarations; the Makefile lists dependencies. | mod name; loads name.rs, and Cargo tracks what each build read. |
The translation unit is one .c file. | The crate is the unit: src/main.rs for a binary, src/lib.rs for a library. |
Understand it
A C project splits declarations into headers, definitions into .c files, and the build graph into a Makefile that must list which object depends on which header. Every function that is not static has external linkage and is visible to any translation unit that declares it, and a missing header dependency in the Makefile gives you a stale build that links old code.
In Rust the unit of compilation is the crate, a tree of modules compiled together, not a single file. There are no headers: mod geometry; in src/main.rs or src/lib.rs tells the compiler to load src/geometry.rs (or src/geometry/mod.rs), and the compiler reads the definitions directly. Because rustc sees the whole crate and Cargo records which files each build read, there is no dependency list to keep in sync.
Visibility is the reverse of C. Everything is private to its module by default, like static at file scope, and pub is the export. Calling a private function from another module is error E0603. pub(crate) exports an item to the whole crate but not to its users, which is what most internal C helpers wanted to be.
Cargo's conventions replace the Makefile. Cargo.toml names the package and its dependencies, src/main.rs is the default binary and src/lib.rs the library. A package may have both, in which case main.rs uses the library through the package name, like any outside user would. cargo build compiles, cargo run builds and runs, cargo check type-checks without generating code, and cargo test runs the tests.
The C habit, written in Rust
This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.
mod geometry {
fn clamp_positive(v: i32) -> i32 {
v.max(0)
}
pub fn area(w: i32, h: i32) -> i32 {
clamp_positive(w) * clamp_positive(h)
}
}
fn main() {
println!("{}", geometry::area(3, 4));
println!("{}", geometry::clamp_positive(-1));
}
Try it
Work out how main.rs reaches a function in src/geometry.rs when a package has both src/lib.rs and src/main.rs, then fix the practice program's call to a private function.
What success looks like: You can name the two declarations that make geometry::area callable from main.rs, pub mod geometry; in lib.rs and pub fn area, and the path main.rs uses.
Check your understanding
A package named shapes has src/lib.rs, src/main.rs and src/geometry.rs. How does main.rs call area from geometry.rs?
Reveal the answer
#include "geometry.rs"at the top ofmain.rs- Mark
areaasexternso that the linker finds it lib.rsdeclarespub mod geometry;,areaispub, andmain.rscallsshapes::geometry::areamain.rscallsgeometry::areadirectly, because both files are insrc/
Files are not compilation units: mod puts geometry.rs into the library crate, pub exports it, and the binary crate uses the library through the package name.
Go deeper
Common misconception: “mod geometry; is Rust's #include.”
#include pastes text into the translation unit. mod geometry; declares a module and tells the compiler where its source is; the module has its own namespace and privacy, and it is compiled once as part of the crate.
- Turn headers into modules: For each
.cand.hpair, create onename.rsand amod name;line in its parent module. - Choose the exports: Mark
pubonly what other modules call; leave the rest private, asstaticwould have. - Pick the targets: Put reusable code in
src/lib.rs, the entry point insrc/main.rs, and call the library through the package name.
Transfer challenge: Run cargo new shapes, add src/lib.rs with pub mod geometry; and src/geometry.rs, and call shapes::geometry::area from src/main.rs; then make area private and read the error.
Connections
- char Arrays to Strings: Owned UTF-8 with a length, not a NUL
- unsafe, Raw Pointers and FFI: Where Rust hands the proof back to you

Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Headers and make to Cargo (courseId "rust-for-c-developers", lessonId "c-make-and-headers-to-cargo") Link: https://crabrix.com/learn/rust-for-c-developers/c-make-and-headers-to-cargo/ Goal: Items are private to their module unless marked pub; src/main.rs is the default binary target and src/lib.rs the library target. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-make-and-headers-to-cargo) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.