lifetime

Rust Independent input lifetimes

Separate borrowed parameters can have separate lifetimes. If the result is owned, as with a count, it need not borrow either input. Elision treats separate omitted input lifetimes independently. Expected output (not executed): 5 Why: The result is a usize and contains no borrowed reference. Common mistake: Adding the same lifetime to unrelated inputs when the output needs no borrowed data.

Formal Syntax & Structure

rust syntaxrust
fn count<'a, 'b>(a: &'a str, b: &'b str) -> usize

Working Example

Example Snippetrust
fn total(a: &str, b: &str) -> usize {    a.chars().count() + b.chars().count()}fn main() {    let first = String::from("AB");    let count;    {        let second = String::from("XYZ");        count = total(&first, &second);    }    println!("{count}");}
Rust Reference