I really liked this tutorial! I thought it might have been interesting to also motivate ownership by showing how singular ownership circumvents dangling pointers, but I that might be more detail than is required to understand how it works. I really liked your kite analogy as well :D
@FiosQuest
3 ай бұрын
Thank you, it literally took me seven years to come up with that analogy. 😅 You're right, there's so many ways in which lifetimes, challenging as they are, are awesome, but with people coming to these videos from all sorts of languages (and even a few non programmers which is awesome) I wanted to keep it as simple as possible. My hope is that people who know about lower level languages recognise the benefits like this, and that those who don't, never have to find out. 😅
@LoZander
3 ай бұрын
@@FiosQuest yes, that makes a lot of sense. Keep on doing the good work :D Also, I'm curious on why you advice against passing mutable references to functions? Usually I'd agree, but specifically because of the implicitness of a function changing a value which exists somewhere else, but in Rust it's rather explicit. Maybe there's something I'm missing or an entirely different reason that it's not good practice generally? :)
@FiosQuest
3 ай бұрын
@@LoZander I might have come off a bit strong in this video, but I'm not saying never do it. In fact, for the homework, you'll need to. 😉 More just "attempt" to avoid it, both for legibility / grokability, and for being more intentional around memory management. For example, if you need to read and modify something, that's fine. Even something as simple as Vec::reverse(&mut self) is a good use of a mutable reference rather than assign a bunch more memory for your reversed vector. But often times a mutable reference can cause subtle issues with memory management. For example, in my opinion, StdIn::read_line ( doc.rust-lang.org/std/io/struct.Stdin.html#method.read_line ) is almost never going to be the right way to access standard in _unless_ you need to append the input to the given String, and even then, you're _probably_ going to cause Rust to allocate new memory and move all the data anyway, you just _might_ get lucky and there's enough capacity in the String to avoid that. None the less, that's how the official book ( doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html ) suggests you get input from standard in, with String::new of all things. Funnily enough this actually won't cause an extra allocation unless the user enters something unexpected in this use-case, but (IMO) we're teaching people bad habits early here, which is why I used .lines() in my version. kzitem.info/news/bejne/l4aHnp-ua4x3qWU
@FiosQuest
3 ай бұрын
@@LoZander These are great questions by the way, thank you. :)
@LoZander
3 ай бұрын
@@FiosQuest Ofc., and thank you for your answers :D I'm not sure I'm completely understanding why the mutable reference specifically is the issue in the example you mention. Is it that the mutability might indicate that the function will do some operation on the data which will cause inefficient memory operations? Like it's a mutability issue wherein its best to evoid mutating generally? Or is it a mutable *reference* specific thing that I'm not getting? Thanks you for your answers :)
@FiosQuest
4 ай бұрын
Erratum and Ellaboration: ===================== I'm definitely going to make mistakes as I make these, I'll pin a comment with anything important. If you spot anything not mentioned here, let me know so I can add it! format! ------------ You don't need to store the result of the format macro in a variable before returning it, and in the script I spent time pointing this out. I dropped it in edit though as I thought the annotations that VSCode adds really help show what's happening. Heap Memory Management --------------------------------------------- Usually the operating system provides memory management, but this isn't always the case. Its possible to write your own memory manager in Rust and this is typically what happens when writing for web assembly or embedded systems where the OS isn't directly available or completely non existent. Big negative impacts on performance ------------------------------------------------------------ I didn't meant to single out Go here, that's just the title of the blog article. You can find similar issues in most (all?) garbage collected languages, but that's not to say these are bad languages. Usually these languages have specific use cases in mind, and they perform fantastically at them. I wouldn't even say Discord necessarily chose the wrong language to start with, the service that they changed was doing its job and doing your job is what makes money. Remember, perfection is the enemy of done. Don't fret _too much_ about what language is the best, choose the one that gets your project up and running adequately the fastest. Copy from the Heap -------------------------------- It is totally possible to put Copy values on the Heap (using a type called Box), and there are reasons you might do this to do with dynamic dispatch. Since we aren't going to cover dynamic dispatch for a while, not to mention, reasons to have to do this are vanishingly small, I didn't really want to delve into this but in hind sight there might have been a better way to word this, or at least I could have said "almost always". You still have to copy the inner value of the Box though, Box itself is not Copy ⚠ Mistake in Split with Substring ------------------------------------------------------- This one is important as it'll impact the homework. In the function, the second slice starts at [found_at + 1 ..] This is wrong, if your sub_string is longer than one byte it won't give you the correct result. 🤦🏻♂ It should be [found_at + sub_string.len() ..] Borrow ----------- Never really explained this but when you pass a reference to data that is owned elsewhere, you are "borrowing" it. You'll see "reference" and "borrow" used somewhat interchangeably throughout the Rust ecosystem. If anyone would like to elaborate on this, please drop a reply :)
@SelectedPeppa
3 ай бұрын
great videos. U lost me wth that lifetime stuff, but thats me beeing old and stupid. The only thing that I want to critique is the music. It gives the video a nice touch, but it is a little bit distracting when you talk over the music. maybe fade it out when talking? Dunno... keep up the great work!
@FiosQuest
3 ай бұрын
Thanks for the feedback! Levelling the music is really difficult, it sounds different on everything you listen to on. If I ever stitch these videos together you'll see they're all over the place too. 😅 Oh dear, I was so pleased with my lifetimes metaphor too 😅 I was going to make it a Short, but I'll have a think about how I can tighten it up. It's definitely not you, lifetimes are the hardest thing in Rust to learn, but I know you can do it! If it helps at all, I _used_ to think of it upside down. The data is owned by a ship, the anchor is a reference and the chain joining it back to the ship is the lifetime. Wherever the chain goes, its always connected back to the ship. The reason we need to annotate them is just so we know which anchor is tied to which ship. If the anchor enters a... umm... function (oh dear) where it isn't certain which anchor is coming out, it can only go as far as the nearest ship in the... erm... stack (yeah none of these metaphors are perfect) 😅 Either way I'm sure you'll find a mental model that works for you, and if you do, please share it back with me! The more ways we can describe something the more it'll help other people.
@SelectedPeppa
3 ай бұрын
@@FiosQuest hey thanks for the effort. You are really helpful. I'll work through my rust book and through your videos, step by step . I'll probably rewatch this one once I have a basic understanding. Thanks for all yor time and effort.
Пікірлер: 12