Crust of Rust: Lifetime Annotations

preview_player
Показать описание
In the 2019 Rust Survey, a lot of people were asking for video content covering intermediate Rust content. So in this first video (possibly of many), we're going to investigate a case where you need multiple explicit lifetime annotations. We explore why they are needed, and why we need more than one in this particular case. We also talk about some of the differences between the string types and introduce generics over a self-defined trait in the process.

And don't worry, I know that what we're implementing exists in the standard library :)

0:00:00 Introduction
0:03:36 Start a rust project
0:05:20 Struct and method definitions for StrSplit and first test
0:09:32 How you decide between a library and a binary
0:10:58 Start implementing StrSplit
0:16:15 When to use match vs if let some
0:17:10 Doesn't compile! missing lifetime specifier
0:20:33 Can I be wrong by specifying lifetimes?
0:21:25 Anonymous lifetime '_
0:23:10 Order lifetimes based on how long they are
0:25:18 Anonymous lifetime '_ (with multiple lifetimes)
0:26:52 Compile error: lifetime of reference outlives lifetime of borrowed content
0:34:45 Static lifetime
0:41:27 Bug when a delimiter tails a string
0:48:07 What is the ref keyword and why not &
0:51:36 What's the * on the left of remainder
0:52:46 What is take() doing
0:54:48 Mutable references are one level deep
0:55:39 Solving a hang with as_mut()
0:57:49 Multiple lifetimes, implementing until_char
1:03:19 Difference between a str and a String
1:08:15 Multiple lifetimes (continued)
1:15:24 Generic delimiter (Delimiter trait)
1:23:14 char length utf8
1:25:30 Standard library split
1:27:39 Q&A

Рекомендации по теме
Комментарии
Автор

This is exactly the level of tutorials that we need. Not the "hello world" all over again.

FlaviusAspra
Автор

Re-inventing parts of the standard library is such a good motivating example! I really can't click with contrived "foo/dog/shape" tutorials. Your explanations of the problems that come up are also very clear. These videos kept me from giving up on understanding rust, and I'm very grateful!

tryashtar
Автор

3:36 start a rust project
5:20 struct and method definitions for StrSplit and first test
9:32 how you decide between a library and a binary
10:58 start implementing StrSplit
16:15 when to use match vs if let some
17:10 doesn't compile! missing lifetime specifier
20:33 can i be wrong by specifying lifetimes?
21:25 anonymous lifetime '_
23:10 order lifetimes based on how long they are
25:18 anonymous lifetime '_ (with multiple lifetimes)
26:52 compile error: lifetime of reference outlives lifetime of borrowed content
34:45 static lifetime
41:27 bug when a delimiter tails a string
48:07 what is the ref keyword and why not &
51:36 what's the * on the left of remainder
52:46 what is take() doing
54:48 mutable references are one level deep
55:39 solving a hang with as_mut()
57:49 multiple lifetimes, implementing until_char
1:03:19 difference between a str and a String
1:08:15 multiple lifetimes (continued)
1:15:24 generic delimiter (Delimiter trait)
1:23:14 char length utf8
1:25:30 standard library split
1:27:39 Q&A

nickkallis
Автор

One of the few videos online that not just a regurgitation of the Rust book. Love your work 👌

ragy
Автор

This was excellent. 90-ish minutes are perfect length for this kind of stream. I also liked the level of difficulty of the topic. I'm not saying stop making longer streams, just more like this! :) Thanks a lot!

mottosson
Автор

For anyone who has doubt about Rust and what it has to offer, Jon has done an exponentially great job of clarifying the reality of what Rust offers and how important it is as a language. He does so without being gimmicky and while being very clear about how to approach the language. He speaks succinctly on the subject matter and provides deeply practical approaches to how he teaches the language. I don't think his value has been realized at the level that it should be but I do believe it will eventually happen. I have learned very valuable lessons from Jon, even with being in nearly 30 year veteran in Software Engineering and Architecture. Not that we ever stop learning but to be learning at the level that I am learning now, shows that a lot of the old ways are outdated, when it comes to programming languages and their approach. Thanks, Jon!

damickillah
Автор

Finally, I've figured out what Rust lifetime annotations are, and how to use it. I've also liked the TDD way you have used here. Congratulations on your awesome job with this tutorial. Thanks a lot!

jaumearus
Автор

I almost gave myself a stroke trying to wrap my head around these lifetime things. This video literally saved me from it. Clear, concise, relevant.

frcrr
Автор

Excellent! An hour ago, I didn't understand the problem lifetimes were trying to solve. After this example and explanation, I immediately see how I can use lifetimes to refactor my code to avoid a bunch of copies.

Jesse_Carl
Автор

Fantastic content as usual. Your videos on Rust are one of the most valuable resources out there for those of us with a handle on the basics. i eagerly look forward to new ones. This focus on intermediate Rust content, along with its shorter length, is really really valuable IMHO. Keep up the great work!

MrJlgerber
Автор

Great lesson. The build up towards the std implementation is logical and gives an impression of how one would come up with a good implemention by iterating towards more general concepts.

Would be awesome if you could find more examples that tie into the standard library in a similar way. One of the main hurdles after finishing some beginner course like the book is getting through the jungle of all the existing std traits. More so in rust than in other languages' standard libraries imo.

halbeard
Автор

I thought I had a decent grasp of lifetimes and references. But I learned several things. Thank you! I appreciate your teaching style, and the level of difficulty of these Crust of Rust videos.

markday
Автор

One nitpick to your explanations – IMO explaining 'static lifetime as ‘living to the end of the program’ is misleading – eg. all owned values not bound by other lifetimes will satisfy the 'static lifetime – eg. if you accept some generic type that is bound by a trait and a 'static lifetime, then you’ll be able to pass owned values there (even though they’re owned and will be deallocated at the end of the block).
I think a much better explanation would be to say that 'static lifetime means that whatever you have that satisfies it will be valid *as long as you hold on to it* (but it might be deallocated later). If that thing is a reference (any &'static _) then it’s true it means that the memory behind it will be valid to the end of the program (because nobody can deallocate the memory pointed to by static references, it is always borrowed), but if you have T: AsRef<str> + 'static, then you might have just owned String – you generally will deallocate the String when you go out of scope (you *might* as well leak it, making it valid to the end of the program but generally you won’t) – but as long as you hold on to the String, its memory is valid, and thus String indeed is valid type satisfying AsRef<str> + 'static – and because of that actually satisfying AsRef<str> + 'a for any 'a. So 'static on a variable means that if you hold on to that variable, you won’t see its memory deallocated, for as long as you wish (if it’s a reference, the memory will live forever, if it’s an owned value – it’ll get deallocated only after you drop it).

benedyktjaworski
Автор

1:33:11 I'm very glad for the digestible length!!! Hoping you continue the Crust of Rust series :)

daniellambert
Автор

I am seeing this from the future, omg, this is a gem. Thank you so much, I was really confused about managing lifetimes. Watching videos that explain lifetimes without going further away from the examples the BOOK shows is really not helpful. This has helped me a lot.

Gordolone
Автор

I once again come back to this video, I once again learn something new. As always, really great stuff Jon. Applicable teaching for various levels of understanding is quite a skill. Thanks again for making these

christopher
Автор

This content is gold! The people asking questions are reading my mind!

letsgetrusty
Автор

I am grateful for the valuable information and knowledge you are sharing with the community. Thank you so so much, Mr. Gjengset.

thetrungtran
Автор

Great video! In addition to learning on lifetimes, I also really enjoyed side notes you provided along the way, like str vs String, and your answers to questions.

TheGokhansa
Автор

Just watched it and it was awesome. This was probably the most practical and approachable way of getting a better understanding of lifetimes. Thank you for your time and effort in doing this. 🙇🏻‍♂️

asaaki