Elision Rules
When lifetimes are inferred.
Elision Rules is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Lifetimes You Do Not Write
Many functions take and return references without any lifetime annotations, yet they compile. That is because of lifetime elision: the compiler infers obvious lifetimes for you.
Elision Is Just a Shortcut
Elision does not remove lifetimes; the compiler still fills them in behind the scenes using a few deterministic rules. When the rules cover your case, you can omit the annotations.
An Elided Function
This function has no explicit lifetimes, yet the compiler understands the returned reference borrows from the input. The rules make it unambiguous.
fn first_word(s: &str) -> &str {
s.split(' ').next().unwrap_or("")
}
fn main() {
let phrase = String::from("rust is fun");
println!("{}", first_word(&phrase));
}Rule 1: Each Input Gets Its Own
The first rule: each elided lifetime in the parameters gets a distinct lifetime. Two reference parameters get two separate lifetimes.
Rule 2: One Input, One Output
The second rule: if there is exactly one input lifetime, it is assigned to all output lifetimes. This covers functions like first_word that take and return one reference.
fn trim_start(s: &str) -> &str {
s.trim_start()
}
fn main() {
println!("[{}]", trim_start(" hi"));
}Rule 3: Methods and &self
The third rule: if a method has &self or &mut self, the lifetime of self is assigned to all output lifetimes. This is why methods rarely need annotations.
struct Holder { value: String }
impl Holder {
fn get(&self) -> &str {
&self.value
}
}
fn main() {
let h = Holder { value: String::from("stored") };
println!("{}", h.get());
}When Elision Fails
If after applying all three rules any output lifetime is still unknown, the compiler asks you to annotate explicitly. The longest function is the classic case: two inputs, ambiguous output.
The longest Function Again
Two reference inputs and one reference output: rule 2 does not apply (more than one input) and there is no self. So you must annotate.
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
fn main() {
println!("{}", longest("abcd", "ab"));
}Multiple Inputs, Output From self
A method with several parameters still gets its output lifetime from self by rule 3, even though other reference parameters exist.
struct Doc { body: String }
impl Doc {
fn announce(&self, _note: &str) -> &str {
&self.body
}
}
fn main() {
let d = Doc { body: String::from("content") };
println!("{}", d.announce("ping"));
}Why the Rules Exist
These three rules cover the overwhelming majority of real code, so most functions need no lifetime syntax at all. You write annotations only when intent is genuinely ambiguous.
A Quick Checklist
To decide if elision applies, ask:
- Is there exactly one input reference? Output borrows from it.
- Is there a
&self? Output borrows from self. - Otherwise, annotate explicitly.
Quick Check
Test your understanding of elision rules.
Recap
You learned when lifetimes are inferred:
- Rule 1: each input reference gets its own lifetime
- Rule 2: one input lifetime maps to all outputs
- Rule 3:
&selfsupplies the output lifetime in methods - If outputs remain ambiguous, you annotate explicitly
Frequently asked questions
Is the “Elision Rules” lesson free?
Yes — the full text of “Elision Rules” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Elision Rules”?
When lifetimes are inferred. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Elision Rules” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.