Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 24 of 50 · rust

Lifetimes

Duration: 20 minutes

Lifetimes\n\nLifetimes ensure that references are valid for as long as they are used.\n\nrust\nfn longest<'a>(a: &'a str, b: &'a str) -> &'a str {\n if a.len() > b.len() { a } else { b }\n}\n\nfn main() {\n let s1 = \"short\";\n let s2 = \"much longer string\";\n let result = longest(s1, s2);\n println!(\"Longest: {}\", result);\n}\n\n

Previous: GenericsNext: Modules and Packages