Slices and Lifetimes
Slices let you reference parts of a collection without taking ownership.
String Slice Example
let s = String::from("hello world");
let hello = &s[0..5];
let world = &s[6..11];
They point to a segment of memory.
Lifetimes
Lifetimes ensure references are valid while used.
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
'a
is a lifetime annotation ensuring both parameters live long enough for the returned reference to be valid.