Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lifetime Errors

Lifetime errors are the most common Rust-specific challenge when migrating from C. They arise because Rust enforces at compile time what C leaves to programmer discipline: every reference must be valid for its entire usage.

Ownership Patterns

PatternRust SyntaxC EquivalentUse When
OwnedString, Vec<T>malloc + freeData has a single clear owner
Borrowed&T, &mut Tconst T*, T*Temporary read/write access
SharedRc<T>, Arc<T>Reference countingMultiple owners

Common C Patterns and Rust Solutions

Returning a Pointer to Stack Data

// C: undefined behavior
char* get_name() {
    char buf[64];
    sprintf(buf, "model_%d", id);
    return buf;  // BUG: pointer to expired stack frame
}
#![allow(unused)]
fn main() {
// Rust: return an owned String
fn get_name(id: u32) -> String {
    format!("model_{}", id)
}
}

Mutable Aliasing

// C: two pointers to the same data
void swap_first_last(int* arr, int len) {
    int tmp = arr[0]; arr[0] = arr[len-1]; arr[len-1] = tmp;
}
#![allow(unused)]
fn main() {
// Rust: use slice methods that handle aliasing safely
fn swap_first_last(arr: &mut [i32]) {
    let len = arr.len();
    arr.swap(0, len - 1);
}
}

Common Lifetime Fixes

Function That Borrows and Returns

#![allow(unused)]
fn main() {
// Error: missing lifetime specifier
fn longest(a: &str, b: &str) -> &str { ... }

// Fix: output lifetime tied to inputs
fn longest<'a>(a: &'a str, b: &'a str) -> &'a str {
    if a.len() > b.len() { a } else { b }
}
}

When to Use Owned Types Instead

If lifetime annotations become deeply nested, consider owning the data:

ComplexityApproach
Simple (1 lifetime)Use &'a T
Moderate (2-3 lifetimes)Use &'a T with clear naming
Complex (nested lifetimes)Use String, Vec<T>, or Arc<T>

Diagnostic Tips

The Rust compiler’s borrow checker errors include helpful suggestions. Look for:

  • “consider borrowing here” – add &
  • “consider using a let binding” – extend the lifetime
  • “lifetime may not live long enough” – add or adjust annotations

Navigate: Table of Contents