Rust's lifetimes made easy

preview_player
Показать описание
Let's figure out Rust's lifetime concept. We'll walk through a small Rust program that uses references and see what Rust means when it talks about a lifetime (and why Rust is so loud when it detects an error).

The speaker further explains Rust’s warnings against reusing references and introduces terms such as 'Box' and the creation of values outside the function's local scope. The video successfully simplifies complex ideas like data allocation, reference modification, and the static lifetime concept.

I hope that this is a useful resource for all developers wanting to deepen their understanding of Rust programming language. Suggestions for future videos are very welcome!

🦀 Rust resources:

👋 Connect with Tim:

👍 Like this video if you found it helpful, and share it with your friends who are also interested in Rust programming.
Рекомендации по теме
Комментарии
Автор

I have the most problems with lifetimes when assigning lifetimes to structs. Whenever a struct holds a reference to something it needs a lifetime spec. Then a cascade of items need a lifetime and it gets quite messy. I just avoid references in structs.

chuckcarlson
Автор

I wrote the code, it works, but I don't understand how it works. that's what lifetimes are to me.

theoutsider
Автор

Explaining it in C/C++: In a function, when you have a pointer to a local variable, don't ever let some object you got from outside keep it, and don't ever return it, because after the function ends that variable dies and now your pointer points at nothing.
Always use a pointer to a heap allocated variable for that.

C and C++ won't bat an eye if you do that, the C and C++ compilers don't give a shit about it. But Rust won't.

yoavmor
Автор

Drawing the stack out is a good visualization technique

goodnewsjohn
Автор

4:30 when should I return something via Box, and when should I choose the static lifetime instead?

GLzdA
Автор

The first video I see about rust lifetimes without practical examples of lifetime annotations 😆

DanielSantanaBjj
Автор

nice video. keep up the great work. quick question: what's the VSCode theme/color palette and font that you were using in the previous video? it's been haunting me :)))

pluagecehmg
Автор

you may learn Rusty's lifetimes in a lifetime

jorgealfonso
Автор

*a_ref (100) + 900 => b (900)
Am I getting something wrong?

TarasShabatin
Автор

I think that no video is near to the brown university book explanation. It's hard way of learning. But it's also nice to do some variation in the learning style.

abdelhakimkhabir
Автор

Why Mutex needs to be enclosed behind Arc? Why not just Rc?
Well I understood it as I wrote the question. It's because mutating the reference count is not safe, even though mutating the Mutex is safe.
So I believe it is safe to conclude that values inside Arc are generally not thread safe, to support Write.
So the values insde Arc should be immutable?
Will be better if you can illuminate on this topic.

TheDelcin
Автор

Learning Rust is so eye opening. Most other languages hide the truely ugly reality of writing programs for computers. They feel like childrens toys after Rust.

beaticulous
Автор

I don't think that doing "Box::leak(Box::new(b))" is a good approach...

methylphosphonofluoridate
Автор

this is the worst explanation i have ever seen

internethistory