The Rust Standard Library is SO Confusing...Until Now!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone Travis here from travis. media so when people first decide to learn rust or they're trying to work in rust without much experience in it they really have a hard time making sense of the Russ standard Library I mean on the surface there's just so much information here and much of it just feels so textbook I mean I'm just trying to figure out how to round a float here but making sense of the standard library is key for rust development in really any language with the standard Library another being go the standard library is like a Swiss army knife at your disposal to do the majority of things you need to do with the language instead of trying to write everything from scratch you have a library of well- tested memory and performance optimized battle tested code pre-written for you to implement by the rust experts and it goes to say that to make use of these amazing tools you need to know how to use it and you need to become very familiar with it so in this video I want to one tell you why you aren't able to make sense of this Library two I want to give you an outline of the standard Library a framework so to speak and some tips on making it your right-hand man or document and three a cheat to always have it a click away so watch this to the end and if it isn't much clearer to you then then you'll at least have the tools needed to make it so let's get started number one now I know this is obvious but I got to say it before I get to explaining the structure and how to read the standard library and all of that but it's this if you haven't worked your way through the Rust book then you're going to have trouble with this Library maybe if you come from C or some other similar low-level language it'll make more sense to you but if you come from JavaScript or python or Ruby or languages like that it's going to look like a foreign language there are structs and traits and implementations and a lot of talk of the stack and the Heap and all of this is going to make it more difficult to understand thus here's the first step for anyone struggling to make sense of the rust standard Library work your way through the entire Rust book chapter by chapter learn the concepts study the examples and most importantly use the links that take you to the standard library to read further on the concepts that you're learning here's an example so here in Chapter 2 we learn about this read line method that puts whatever the user enters into the string we pass to it but it also returns a result value what's a result value well it tells you here result is an enumeration blah blah blah blah but you shouldn't just do that it's so tempting just to read this and keep moving on and say I'll learn about it later but here's what you should do instead that result word is a link open the link and it takes you to the standard library and it tells you that this is an enum and it has two variants but what else you should be doing is learning where this is at in the stand standard library for when you need it again so here I see that it's the standard library and the result module so let's go back to the main page let's scroll down and find modules and then down here we'll find result click on result and here's the page but this isn't the exact same page to find what they linked to you need to scroll down past all the examples down into the strs and then the enums down here in enums here's the result enum click on that and you're at this page so so you not only learned what result is but you also learned where it's at in the standard Library here's another example so we have a string string is a type provided by the standard library that is a growable UTF encoded bit of text instead of just reading that and moving on why don't you click on the string link and see where it's at in the standard library and here we can read about the string we can see examples but the key here is to find out where it's at in the standard Library up here it tells us that it's in the standard Library the string module and then the string struct let's see if we can find that let's go back to the main page let's scroll down to modules and find string here's string and this isn't the same page they linked to so let's keep scrolling down and under structs we'll find the string struct then from there we can scroll down find implementations like the example they gave us of new the new function so the point here is not only to learn the concepts while you're working through the book but to also find out where this stuff is at in the standard Library so the best thing you can do up front is just to do the work of working front to back through the Rust book it's completely free I'll put a link below if you want to work through it with other people in a community then be sure to check out the impostor devs Community where we work through a chapter of this together every week I'll put a link to that below but if you reference the standard Library while you're working through this book I guarantee it'll make a 100 times more sense when you come out on the other end of it now before we get to number two which is an outline or framework for this standard Library let me quickly mention today's sponsor brilliant who can help you supplement your rust learning with computer science principles brilliant.org is a great way to learn Math logic and computer science interactively brilliant is fun practical and has thousands of lessons from basic to Advanced topics from computer science and programming algorithms python AI logic and other tools to help you level up your skills or keep those skills sharp and it's built for busy people like me and you like I said you can Master big Concepts in as little as 15 minutes a day maybe you're having a hard time nailing down data structure structures like linked lists or accounting operations brilliant will help you visualize and internalize these Concepts in an engaging interactive way today I spent 10 minutes continuing the introduction to algorithms course where I learned how algorithms manipulate and store numbers how arrays work and how to go about searching them and like I said brilliant can help you solidify those coding skills and Concepts that can apply across different programming languages you can get started today for free for 30 days by using the link below and the first 200 to sign up using the link will get 20% off in annual subscription that link is brilliant.org Travis media now back to the video all right next let me give you a quick outline a breakdown of how to read and navigate through the standard Library so let's go to the main page and we'll see here just a basic definition the rust standard library is a foundation of portable rust software a set of minimal and battle tested shared abstractions for the broader rust ecosystem great read that how to read documentation read that but this next section is what I want to touch on there's four main parts to the standard Library there's actually five first we have modules so first of all the rust standard library is divided into a number of focused modules all listed further down on the page these modules are the Bedrock upon which all of rust is forged and they have Mighty names like standard slice and standard compare modules documentation typically includes an overview of the module along with examples and are a smart place to start familiarizing yourself with the library need examples start with modules so that's number one let's scroll down and we'll find a section called modules and here we can pick one like the io module and you'll find lots of examples like read and write if you keep scrolling down you'll find the standard input and output examples this is literally the exact code that you use in Chapter 2 of the Rust book so those are modules that's the Bedrock of the standard library and where you'll find most of the examples that you're looking for number two are the Primitive types so second implicit methods on primitive types are documented here so if you scroll down right above the modules you'll see the Primitive section here's the Primitive types of rust so you have the array the bull the Char the floats the integers the string the tupal all of the Primitive types are here and if you want to know what methods you can call on them then just pick one like let's choose bull for Boolean so here's the definition here's some examples and if you scroll down you'll get to the implementations so that's the second item primitive types third is the rust Prelude so third the standard Library defines the rust Prelude let's click on that A small collection of items mostly traits that are imported into every module of every crate the traits in the Prelude are pervasive making the Prelude documentation a good entry point to learning about the library so the items in the Prelude are pulled into every crate you don't have to use the use statement to bring it in from the standard Library they're brought into every crate and we clicked on that link and you can read about the Prelude here so that's number three the Prelude number four four are the macros so finally the standard Library exports a number of standard macros and lists them on this page scroll down you'll find the section on Macros one that you might be most familiar with is the print line macro click on that and we get information on it now the fifth item that I'd want to add to this is the keywords right below that are the keywords so here you'll find a sync if you're doing a loop you'll find break you'll find continue here you'll find the match keyword the loop keyword and all of that and those are the five main items of the standard Library the modules the Primitive types the rust Prelude the macros and the keywords it's all right here on the page now here's a little homework for you go back up to where we were in this next section says a tour of the rust standard Library so go and read the section a tour of the rust standard Library there's containers and collections platform abstractions and IO and use before and after main read these sections and over the next couple of weeks click on these links and get familiar with these terms what they are are and where they're found in the library so here you read that St Str a UTF string slice is a primitive type cool and the standard Library defines many methods for it rust Str Str or strings are typically accessed as immutable references see the reference Ampersand here use the owned string for building and mutating strings so there's a difference here there's string and there's Str Str what's the difference we'll check it out in the standard Library so that's your homework read through the Section a tour of the rust Stander library and over the next couple of weeks look these links up and know where to find them in the library now the last thing I want you to do is to check out in VSS code the rust analyzer extension now not only does this extension give you all types of hints like type hints and parameter hints but it also links you directly to the documentation on your local machine so here you see string this is not something I typed this tells me that this is a string you'll see here result I didn't type this but it tells me that this readline method returns a result but what's neat is I can hover over something like string and I get this documentation so I find out what a string is and I get examples on how to use it also if I hover over the new function I get a definition I get some examples and then I get this goto string now if I click on this it takes me here well what is this well this is the actual rust standard Library documentation if I scroll up to the top you'll see here a UTF encoded growable string here's some examples all of that if I go back to the documentation for the string you'll see the exact same thing it's literally the documentation if I click on Source I should see the exact same formatted thing with all of these commented paragraphs so download that extension and it'll give you the documentation literally a click away and if you get tired of seeing all these helpers like the string and the result type and what's being returned and all of that then this extension gives you the option to disable those if you go to the rust analyzer documentation you'll find settings like these where you can just set them to false and all of that will go away and you can still use the documentation hovering over string hovering over IO standard in my documentation is still there so I hope this was helpful to you I hope this helps you dig deeper into the standard Library which is key to becoming a really good rust developer what do you think have you had trouble with the standard library has it been really confusing to you let's discuss it down below if you found this video helpful give it a thumbs up consider subscribing to the channel if you haven't and I'll see you in the next video thanks for watching that
Info
Channel: Travis Media
Views: 25,384
Rating: undefined out of 5
Keywords: rust standard library, rust programming, learn rust 2024, standard library, rust programming language, rust programming 2024
Id: ODk38qJ1A3U
Channel Id: undefined
Length: 11min 44sec (704 seconds)
Published: Sun Feb 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.