Lifetimes in Rust

It was always there, but now it's explicit

Recap: References

Motivation


	let x;
	{
		let y = 5;
		x = &y;
	} // uh oh
	println!("x: {}", x); // hmmm
	

Rust will not compile this code

Lifetimes

Annotation Example

The borrow checker will use the shortest lifetime

Struct Example

Challenge: Circular references

Rc + Refcell

Vec + Index

3rd party libraries

slotmap: Like a hashmap, but with unique keys

typed-arena: Arena allocator. Can only drop the whole arena