C and Rust, side by side
const; using the counter after counter_finish compiles.#include <stdio.h>
struct counter { const char *label; unsigned hits; };
void counter_hit(struct counter *c) { c->hits++; } /* may modify */
unsigned counter_hits(const struct counter *c) { return c->hits; } /* reads only */
unsigned counter_finish(struct counter *c) { /* "do not use c after this" */
unsigned n = c->hits;
c->label = NULL;
return n;
}
int main(void) {
struct counter c = {"visits", 0};
counter_hit(&c);
counter_hit(&c);
printf("%s: %u\n", c.label, counter_hits(&c));
printf("final: %u\n", counter_finish(&c));
counter_hit(&c); /* compiles: the contract lived in a comment */
return 0;
}
hit takes &mut self, hits takes &self, and finish takes self, so the counter cannot be used after it.struct Counter {
label: String,
hits: u32,
}
impl Counter {
fn new(label: &str) -> Self { // an associated function, like counter_init
Counter { label: label.to_string(), hits: 0 }
}
fn hit(&mut self) { self.hits += 1 } // exclusive access
fn hits(&self) -> u32 { self.hits } // shared, read-only access
fn finish(self) -> (String, u32) { // takes the value itself
(self.label, self.hits)
}
}
fn main() {
let mut c = Counter::new("visits");
c.hit(); // auto-borrows: Counter::hit(&mut c)
c.hit();
println!("{}: {}", c.label, c.hits());
let (label, total) = c.finish(); // `c` is moved: no use after this
println!("final {label}: {total}");
}
| C | Rust |
|---|---|
const struct counter * documents read-only access. | &self is a shared borrow; no one mutates the value during the call. |
Do not use the counter after counter_finish is a comment. | fn finish(self) consumes the value; a later use is E0382. |
counter_init(&c) fills memory the caller provides. | Counter::new() returns an initialised value; c.hit() borrows automatically. |
Understand it
C groups behaviour by naming convention: counter_hit(struct counter *), counter_hits(const struct counter *) and counter_finish(...), with a comment that says not to use the counter afterwards. The const documents read-only access, and the do-not-use-after rule is not checked at all: the example calls counter_hit after counter_finish, and it compiles.
In Rust, functions associated with a type go in impl Counter { ... }. The receiver is the first parameter and carries the contract. fn hits(&self) is a shared borrow, like a const pointer with the stronger guarantee that nobody mutates the value during the call. fn hit(&mut self) is an exclusive borrow. fn finish(self) takes the value itself: after c.finish(), c has moved, and calling c.hit() is error E0382.
Method calls borrow automatically: c.hit() means Counter::hit(&mut c), so there is no & at the call site and no -> operator; . works through references. A function without a receiver, such as Counter::new, is an associated function, the idiomatic replacement for counter_init, and it returns the initialised value instead of filling memory the caller provides.
Fields are private to the module unless marked pub, so the invariants behind methods such as spend and remaining can only be broken by code inside that module. That replaces the opaque-pointer pattern, struct budget; in the header, for most C APIs.
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.
struct Counter {
hits: u32,
}
impl Counter {
fn hit(&mut self) { self.hits += 1 }
fn finish(self) -> u32 { self.hits }
}
fn main() {
let mut c = Counter { hits: 0 };
c.hit();
let total = c.finish();
c.hit();
println!("{total}");
}
Try it
Port the commented C budget: spend refuses an amount larger than what remains and then changes nothing, remaining reports the rest, and close consumes the budget and returns the amount spent. Avoid the C version's wrapping sum.
What success looks like: Spending exactly the remainder succeeds, a refused spend leaves the state unchanged, and spend(u32::MAX) is refused without overflowing.
Check your understanding
Which receiver should close take so that using the budget after budget.close() is a compile error?
Reveal the answer
&selfself&mut self&'static self
self moves the value into the method. The caller's binding has moved, so any later method call on it is E0382.
Go deeper
Common misconception: “self in Rust is like this in C++: always a pointer.”
self by value is the whole struct moved into the method; &self and &mut self are the borrowed forms. The receiver is chosen per method and is part of the API contract.
- Read the receivers: For each method, note
&self,&mut selforselfbefore reading its body. - Mark the consuming calls: After a call to a
selfmethod, the receiver binding has moved; check that nothing uses it later. - Check the invariant: Keep fields private so only the module's methods can change them, then test each method at its boundary.
Transfer challenge: Add fn reset(&mut self) and fn split(self, part: u32) -> (Budget, Budget) to Budget, then predict which later calls compile after each one.
Connections
- Struct Copies: Copy and Clone: Bitwise copies only for plain data
- Tagged Unions to Enums: A tag that cannot disagree with its payload

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: Structs and Methods (courseId "rust-for-c-developers", lessonId "c-structs-to-impl") Link: https://crabrix.com/learn/rust-for-c-developers/c-structs-to-impl/ Goal: &self reads, &mut self modifies, and self consumes the value; read a method's receiver to learn its ownership effect. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-structs-to-impl) 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.