Skip to content

Instantly share code, notes, and snippets.

@ognis1205
Created April 26, 2024 12:38
Show Gist options
  • Save ognis1205/ec0dbf4357cf4e73b6f616bb2d3c6fe2 to your computer and use it in GitHub Desktop.
Save ognis1205/ec0dbf4357cf4e73b6f616bb2d3c6fe2 to your computer and use it in GitHub Desktop.
// Accepts any type T as its argument which satisfies 'static lifetime trait bounds.
fn generic<T: 'static>(v: T) {}
// 1. No references.
struct NoReferences(String);
// 2. Includes a 'static lifetime reference.
struct IncludesStaticRef(&'static str);
// 3. Inclues a lifetime reference named 'a
struct IncludesNamedRef<'a>(&'a str);
fn main() {
generic(NoReferences("abc".to_string())); // -- (1)
generic(IncludesStaticRef("abc")); // --------- (2)
generic(IncludesNamedRef("abc")); // ---------- (3)
{
let x: String = format!("abc");
generic(IncludesNamedRef(&x)); // --------- (4)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment