Skip to content

Archive

Ownership

5 articles
Rust 03 Sep 2026 9 min read

Understand Pin and Unpin in Rust

Most Rust values can move freely. Assign a value to another variable, pass it by value, return it from a function, or replace it inside a container, and the value may end up at a different memory address. Usually that is exactly what you want. Rust’s ownership model tracks who owns a value, not where that value must remain in memory. A smaller class of types is different. Some values become address-sensitive: code relies on the value continuing to exist at the same memory location. Compiler-generated futures and carefully designed self-referential structures are common examples.

Rust 02 Sep 2026 4 min read

Rust Iterator Ownership: iter, iter_mut, and into_iter

Rust iteration becomes much easier once iterator choice is connected to ownership. For a collection such as Vec<T>, the central question is whether the loop should borrow values, mutate them in place, or consume the collection. The common methods are iter(), iter_mut(), and into_iter(). Borrow with iter() iter() produces shared references: fn print_names(names: &[String]) { for name in names.iter() { println!("{name}"); } } Inside the loop, name has type &String.

Rust 02 Sep 2026 4 min read

Interior Mutability in Rust with RefCell

Rust normally enforces borrowing at compile time: either one mutable reference or any number of immutable references may exist at a given moment. That rule prevents data races and many aliasing bugs before the program runs. Sometimes the compiler cannot prove that a safe mutation pattern is valid, even though the program can enforce the rule dynamically. RefCell<T> provides interior mutability for those cases by moving borrow checking from compile time to runtime.

Rust 01 Sep 2026 5 min read

Choosing &str, String, and Cow for Rust Text APIs

Rust has several common ways to represent UTF-8 text, and choosing between &str, String, and Cow<'a, str> is fundamentally an ownership decision. The best API is usually the one that asks callers for the least ownership it needs and returns ownership only when the result requires it. Use &str when you only need to read text A string slice borrows UTF-8 text owned elsewhere: fn is_blank(value: &str) -> bool { value.trim().is_empty() } This accepts borrowed views into a String, string literals, and other string slices without taking ownership or allocating.

Rust 01 Sep 2026 4 min read

Borrowed or Owned Data in Rust API Design

Rust APIs frequently face a design choice that is more important than syntax: should a function borrow data from the caller or take ownership of it? Borrowing can avoid allocation and make reuse cheap. Ownership can simplify storage and decouple lifetimes. Good APIs use each where it matches the actual data flow. Borrow when work is temporary If a function only reads a string during the call, accepting &str is usually natural: