From 3df4f2a63f176d0f947cee4860d65c9153b8debd Mon Sep 17 00:00:00 2001 From: "Champlin, Saji" Date: Tue, 6 May 2025 10:34:49 -0500 Subject: [PATCH] add rust lifetimes presentation --- content/decks/rust-lifetimes.html | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 content/decks/rust-lifetimes.html diff --git a/content/decks/rust-lifetimes.html b/content/decks/rust-lifetimes.html new file mode 100644 index 0000000..5ee499f --- /dev/null +++ b/content/decks/rust-lifetimes.html @@ -0,0 +1,142 @@ +
+

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

+
+
    +
  • + safety of ownership/mutability at runtime
  • +
  • - there is a performance penalty
  • +
  • - a bit awkward to use.
  • +
+
+
+

Vec + Index

+
+
    +
  • + fast, no weird Rc stuff
  • +
  • - bug prone, self managed, can dangle
  • +
+
+
+

3rd party libraries

+

slotmap: Like a hashmap, but with unique keys

+

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

+
+ +
+ + +