Rust Crash Course | Rustlang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] this video is sponsored by Ed you onyx they're running an exclusive sale for traversée media subscribers on their best-selling courses which are available at just seven dollars for a very limited period of time you can learn skills such as machine learning and NLP web development programming languages like C++ C sharp and Java DevOps technologies such as docker and much more so click the link in the description below to check out edgy onyx courses and start building your skills in 2019 hey what's going on guys welcome to my rust crash course so this is gonna be an introductory course on the fundamentals and the syntax of the rust programming language now normally I'll use some slides in my crash courses for frameworks and libraries however when we're dealing with an entire programming language there's quite a bit to get to even though we're just covering the basics so I'm just going to talk a little bit about what rust is and then we'll jump in we'll start looking at the core syntax how to install it look at some of the concepts and so on alright so what exactly is rust it's an extremely fast and powerful programming language rust is best known for being a systems language some other examples of systems languages are C C++ golang these are very powerful and fast languages built more for systems rather than application programming languages like Java c-sharp JavaScript and so on so application programming is basically user-facing software programs that the users interact with things that you would build with a systems language like rust or C++ would be more like drivers and compilers many tools that programmers use in development are built with systems languages so rust is great for that stuff however I know a lot of you guys are web developers obviously if your subscribers to this channel rust is starting to become really relevant in web development because of web assembly ok web assembly is in its early stages but looks really promising if you've never heard of web assembly or wasm it's an efficient low-level byte code for the web ok so it's going to allow us to build very secure portable and very fast application web applications utilizing languages like C++ and of course rust so if you want to get into web assembly but you don't want to work with C++ which is a very very difficult language to work with then rust is perfect so rust and web assembly integrate with existing JavaScript tooling supports you know ACMA script modules as well as tools like NPM and web pack I do have plans on creating a rust and web assembly project on YouTube so this is going to be sort of a precursor to that ok so if you're a web developer wanting to learn what rust and wasm or you're just looking to learn a systems language to build whatever compilers drivers stuff like that then this is kind of an introductory to that all right now one more thing I want to talk about before we get started is garbage collection one of the biggest advantages to rust is that it doesn't have garbage collection and you also don't have to manage memory so and let me explain what that means now in JavaScript for example very often it goes through and it looks for variables and objects and other things that are not-- that no longer have a reference and are no longer needed within that code and it deletes it from memory and it frees up that space so this can sometimes take multiple seconds depending on the program depending on you know what's being collected with languages like C and C++ you handle all this yourself which makes programming much more tedious much harder you have to manage all the memory the allocation and all that stuff it's very difficult now rust works in a different way than both of these you don't have to manage it yourself but instead of it automatically checking every few milliseconds or whatever it checks on demand when needed ok so if the memory heap is close to being full or above some threshold it will then look for variables and so on to free up memory and the compiler takes care of all this for us so it's very efficient and at the same time it's not leaving it up to you to allocate memory and all that stuff that you would do with you know C++ so you kind of get the best of both worlds and I think that's what makes rust an excellent language and rust has its own package manager called cargo which is similar to you know NPM for node or composer for PHP pit B and V for Python every just about every language has its own package manager to install packages to track dependencies all that stuff so we're gonna get into cargo as well which comes with rust now to get started just hit this button here if you're on Windows I believe there's an installer that you'll just need to go through on Mac and Linux you just have to grab this curl command open up a terminal paste that in there and run it and just hit one to proceed with the default installation and it will install it mine went really fast because I already have it installed now you might need to just restart your terminal if you do a let's say rust up - - version and it says not found then just restart your terminal now it comes with a couple utilities rust up is - it's basically a version manager if you want to check for updates you could do rust up update and that should see if there's any updates and then you know update if needed then Russ C is the compiler so you can see that we have that installed as well and then cargo is the package manager okay now what I'm going to do is I'm going to close this up and jump into vs code this is the text editor that I'll be using now very important if you're using vs code you want to install the rust RLS extension okay so go ahead and search for rust and it's gonna be the first one and install that this has you know code completion it's basically a linter for your rust code so you definitely want that so once you have that installed I'm just in an empty folder called rust sandbox and I'm gonna be using cargo but first thing I want to do is just show you how to create a rust file and compile it so I'm going to open up without cargo so I'm going to open up a terminal here integrated terminal and I'm just going to create a new file called hello dot RS and we're going to open that up and inside of our rust application we need an entry point we need a main function so we're gonna say FN main ok so we're creating a function called main and then I just want to print out hello world so the the command that we're going to be using for the most part in this course to print out stuff is going to be print line a println exclamation ok and then in here we're just gonna put in some double quotes and we'll just say hello world and save alright now if we want to compile this and run it we can use the rust C utility directly so we can just say rust C hello and then the file name hello dot RS so we'll run that and now you can see it's created this executable up here so if I want to run that directly from the command line it can simply do dot slash hello and it runs the application and we get hello world in the console ok so I mean that's you know you can create a rust file you can compile it with rusty however that's not really what how you would do it in the real world you want to initialize your project with cargo and do things that way so I'm gonna just delete both of these files right here all right now to initialize a project with cargoes is if there's a couple different ways we could do cargo new and then a folder name like hello and what that would do is create a new project in a folder called hello however I want to initialize everything in this rough sandbox folder so I'm gonna do cargo in it alright and then if we look over here you'll see that we have this file structure and we have a cargo dot Tamil file now I have an extension called better Tamil which will give us the the highlighting here if you don't have it this will just be all white now this has just some information like the name the version very similar to like a pip file for python or a package jason for node it just has all your application info and any dependencies that you have ok so i'm not going to touch this file i just wanted to show it to you and then it gives us a git ignore which has the target folder because that is basically when we when we run the compiler our files will go into there and that's not something you want to push to github or wherever you're pushing it and then our source folder is where all of our rusts code goes so you can see there's a main dot RS file that basically just has the same thing that we created in the hello file now when we want to compile this we don't use the rusty utility directly we use cargo and we can say cargo run what that's going to do is it's going to compile it and it's also going to run it you can see hello world is printing out here now it compiled into this target folder and then into debug and right here Russ sandbox okay so if I wanted to run this I could go into we could say dot slash target's slash debug slash rust underscore sandbox and that will run the actual executable now if you just want to build it out you don't want to run it you can do cargo build okay so you can see it's not printing out hello world however it did build it now if you want to build for production then you want to do cargo build - - release ok when you do that you'll see it says finished release optimized kay so it'll optimize it for production and then in target you'll see there's a release folder and then that's where the the executable is ok for deployment so that that takes care of compiling now I'm going to clear this up and I'm gonna structure this folder in a way that you can go back to it for reference so we're gonna create a sandbox and I'm gonna create a new file for every basically every topic that we cover so first topic I want to cover is this print line command and formatting so in the source folder I'm going to create a new file called print dot RS now what we can do is create a function in this print file and then run it in our main main RS files so I'm gonna go pub which is means public so public function because I want to be able to access this from outside public function and for each file I'm gonna create a run function just to run it and let's first just do a print to console so I'm going to do the same thing just do a print Ln exclamation and let's just say hello from the print RS file okay so we'll save that and then in main dot RS what we want to do is go above the main function and just simply say mod and then the name of the file which is print okay and by the way semicolons are required and rust and then down here let's get rid of this and we can simply say print double colon and then the function and you'll see it even it even has a drop down with the run function okay so if I save that we go down here we say cargo run we get hello from the print print dot RS file okay so that's what we'll do is for each topic we'll create a new file we'll bring it in and we'll run it so let's head over to the print RS and I want to talk a little bit about formatting okay now in many languages you can do like you know you could print out for instance an integer or a number like this and you'll see we're even getting a linting error here but if I save this and I try to run this we get an error here it says you might be missing a string literal to format with excuse me and it even gives us the code that we should be writing now this is what we have to do is use basically a placeholder for anything that for any variable or number or even string anything at all that we want to replace we use these curly braces so we would have to do that and I'm sorry we want double quotes has to be double quotes and then we'll say like number and then let's say we want to put the number one here so we would put that and then the second parameter we would put one okay so I'll save it you'll see that the red line cleared up and if we run cargo run we get number one okay so you can't just directly print line an integer or anything like that now let's say we wanted to have multiple placeholders like let's say we wanted to do say like Brad is from mass so over here I would say Brad mass okay now this is going to be replaced with whatever the first value is over here this will be replaced with the second ok and I use strings here but this could be a number this could be you know an array index whatever so let's save that let's run it and we get Brad is from Mouse okay so this is formatting I'll say basic formatting now we also have positional arguments all right so I'm going to do a print line and if I if I do autocomplete you'll see it puts it there for me but let's say we want to do like Brad is from mass and Brad likes to code okay so I want those variables now I'm using Brad twice so in this case I would use positional parameters over here let's do Brad mass and let's do lowercase code alright so this first one I want to be Brad which is going to be this the zero index okay so is from mass which is the second one which has an index of one and then I want proud again so I want to use zero and then code is in the index of two ok it's zero one two so let's save that and I do have the prettier extension installed and BS code so it might just kind of format it sometimes like that so let's run that we get Broadus for mass and Brad likes two codes so those are positional arguments we also have named arguments okay so let's do a print line and let's say we can actually put names in here so I'm gonna say name likes to play activity and then I'm gonna put in here let's do John and let's do I'm sorry this is wrong we want to actually set these named parameters so we'll say name equals John and let's do activity equals baseball alright so we'll save that and if we go ahead and run this we get John likes to play baseball so we can have named parameters in here if we want okay so you can do positional or named now there's also something called traits or placeholder traits I'm not gonna go over all of these but I just want to show you that there are these do exist so I mean this there's one for like binary hexadecimal octal so let's do a print line and let's do will say binary and the trait for binary is going to be : B and let's do X or hexadecimal actually we want to be in here so hex we're gonna do : X and let's say octal it's gonna be : oh and these are all lowercase and then we're gonna just get these four for the value of 10 10 and 10 okay so it's going to give us the binary for 10 for this one have the hex for this one and octal for this one so let's run that and there we go so there's the binary hex and octal now we also have the debug trait which is which comes in handy if if you want to print out like an entire array or something like that so let's say placeholder for debug traits and we'll be using this quite a bit so we're gonna say print line and this is going to be colon and then a question mark now with this I can actually put in multiple values so I'm gonna put another set of curly braces here and let's put like a number I'll say we'll put a boolean and I'm gonna go over datatypes in the next in the next file and then let's put a string of hello okay so if I want to print all those things out go ahead and run it and there we go okay and this is actually called a tuple so I think that that's pretty much it oh we can do basic math - I guess I'll put this in here so say print line and let's do like ten plus ten equals and then over here we can actually put in the expression ten plus ten okay and whatever the whatever this equals is going to get put into this right here so it saved that let's run that and we get ten plus ten equals 20 all right so I think that's good for formatting and for this print line we'll be using this all throughout this course so let's close that up and let's see the next thing I want to look at actually before we look at types I want to look at variables just how to create them we have variables are immutable by default which is something that's a little different than a lot of other languages so let's go ahead and create a new file here called VARs dot RS and let's create our run function and I just have some stuff that I want to paste up here just some information I might do this in a couple different files so variables hold primitive data or references to data variables are immutable by default meaning that by default you can't reassign them okay rust is also a block scoped language meaning that you know if you set a variable in a function it sits it pertains to that scope all right so let's go ahead and create a variable now we use the let keyword okay and if you're a JavaScript developer this probably looks very familiar so we're gonna say let name equals and let's set to Brad okay now I'm going to go ahead and do a print line here and I'm gonna say my name is and then over here we'll put in a name okay remember we can't just we can't do print print line and just put in name who won't let us do that but let's save that go to main and I'm going to comment out this and we want to bring in VARs okay and then we want to go down here and I'll go ahead and comment this out and let's pray let's save VARs run okay and then we'll go down here and we'll do cargo run and we get my name is Brad now if I want to reassign this if I say name actually know what we'll do we'll keep that let's let's do another variable and let's do age okay because name is something that probably won't change age however will so let's say age equals 37 all right and then down here I'll say my name is Brad and I am and then we'll put in here age okay so that will work my name is Brad and I'm 37 but let's say I have a birthday and I want to change age now to 38 but go ahead and run that if I do that we get this error cannot assign twice to a mutable variable okay so a mutable basically means you can't mutate it you can't you can't reassign it and for your JavaScript developers it's basically like using Const now we can make this mutable all we have to do is simply add excuse me is add mute okay mu T now if we save this and run it it says my name is Brad and I am 38 now we do get a warning just saying value assigned to age is never read because where we where we put it to 37 we never used it when it was 37 however if I take this and I copy it and put it up here and we use that when it was 37 we won't get that warning anymore okay so now we get 37 and 38 so that's how immutable variables work and rust now there is actually a Const keyword as well what's going on here clear this out so there is a constant use that much but if they do exist so let's go ahead and say define constants okay so we can say Const and let's do ID usually an ID doesn't change now when you use kant's you do have to explicitly define a type so let's define this as an integer 32-bit okay and we're going to set this to 0 0 1 now down here I'm just going to do a print line and let's say ID and then we'll go ahead and here and we'll put in ID okay so we save that that should work so you'll see ID one so we can use Const usually it's going to be all uppercase when you do use Const and you do have to add a type all right now last thing I want to show you in this file is that we can assign multiple variables at once okay so I can do for instance let's and then some parentheses and I can say like let's do my name my age we can set that equal to another set of parentheses and let's do Brad and 37 and then we'll go ahead and do a print line and we'll say curly braces is curly braces and then here we'll pass in my name my age all right so let's run that and then down at the bottom here you'll see Brad is 37 so we can assign multiple variables at once okay so now that we've gone over how to assign variables how they're immutable but we can make them mutable let's look at data types so I'm going to create a new file called types dot RS keep wanting to put j/s and then let's go ahead and do pub function run and in our main jet main RS we're gonna copy this down comment that out and let's bring in types okay and then we'll run types run okay so in this types file I'm just gonna paste in some stuff here so I'm gonna paste in the primitive types of rust okay so as far as integers we have unsigned integers of different bits and then we have just regular integers so most commonly you'll probably use I I 32 unsigned just means that there's no negative values it's a you can't have negative numbers integers can be positive or negative and the number is just a number of bits they take in memory okay so the larger the number the more the bits floats you have 32 and 64 boolean is bool you also have char or a car which is just it's one character okay it's not a string strings are kind of weird actually in rust and I'm gonna talk about strings afterwards you also have two poles which are basically lists and then you have arrays which are also primitive types but a raised in rust are a fixed length okay you have something called vectors which are basically growable arrays and we'll we'll talk about those after I'm not even going to get into tuples and arrays and this file err but these are primitive types of rusts now I'm gonna paste in another little tip here and that is that rust is a statically typed language which means that it must know the types of all variables at the time of compile however the compiler can usually infer what type we want to use based on the value and how we use it so it's not required that you set the type for every single variable you create it's going to infer what that type is gonna be you know sometimes it might be wrong and it might complain but you know for instance if I do let's we'll do let x equals 1 so by default this is going to be in I 32 okay integer 32 I don't have to I don't have to explicitly define that now for a float let's do let y equals and we'll say 2.5 so by default this is going to be a float 64 so it's going to be F 64 by default now if we want to do an explicit type so let's say add spell explicit type so let's say we want to do in I 64 so I'll say let Y and then to add a type we do a colon space and then let's do I 64 and we'll say equal and we'll do some large number okay so we can do that now if you want to find the max size of let's say an integer of 32 or 64 we can do that so let's say find max size we can do print line and let's say max I 32 and then we want for the place holder we're going to use STD so basically the standard library we need to bring this in so we're gonna say STD double colon I 32 and then double colon max okay so that will give us the max for I 32 and then I'm gonna copy that down let's do the same for 64 all right so save that and it's go to main RS oh we already did that okay so let's run cargo run consider using underscore oh I used Y twice we'll just call this one Z to get rid of that warning but anyway if we look down here this is the max for an in I 32 so if you're gonna use a number larger than this then you want to use I 64 okay and you can look with the max for like floats and all that stuff as well if you want so next we'll take a look at setting a boolean so a boolean is obviously true or false so we'll do like let is active and we'll set that to true and down here let's do a print Ln and let's use our debug placeholder here so colon question mark and we'll just print out some of this stuff that we've we've created up here so let's do like X Y Z is active okay we'll go ahead and run that and we get 12.5 this big number here and then true now for the boolean it it inferred that it's a boolean but we could explicitly set it so I could do bool like that okay so that'll still work let's see we can also get a boolean from an expression so if we were to do like let is oh by the way this the convention for variables is underscore you're not going to see like camelcase too much here in rust so let's do is greater is greater and let's set that to 10 is greater than 5 okay so what should happen is it should evaluate this and put that value either true or false in here and we can explicitly set this if we want like that and then let's put in his greater okay so we'll clear this up or in cargo 1 cargo run and we get true if I were to change this to less than and we run it now we get false ok so last thing I want to show you is a character or char which is a unicode character so for instance let's say let a 1 and set it to and we're going to use single quotes this is how you signify a car or char with single quotes so if I go down here and let's put in a 1 excuse me and we run this we get a as a char now it has to be just one character if I put a be you'll see it already gives me an error character literal may only contain one code point now this could be any Unicode in fact emojis are Unicode so I could even do like let face and we can specify this with a slash u so we're going to say unicode and then inside curly braces I'm going to use 1f 600 and this is like it's a smiley face or something like that so I'm going to save actually let's go at it down here so we can see it and we'll say face and then let's clear this up and run it and you'll see that we get a little smiley emoji face okay and you can look these unicode's up online just type in a mode unicode and you can find these alright so i think that's good for primitive types so now we're gonna look at strings talk about those for a little bit and also look at some string methods so i'm going to create a new file and i'm going to call it strings dot RS and let's create our public function run okay and we'll go ahead and bring this into main dot RS so i'm gonna copy this down and let's bring in strings and then down here we want to run strings run okay so i'm actually going to just paste something in here real quick that i want to talk about so there's there's two types of strings really there you have a primitive string which is immutable it's a fixed length string somewhere in memory so what we've been doing with strings so far we've been using primitive strings but you also have the string type which is a growing heap-allocated data structure okay that you can use when you need to modify our own string data so if you want to push something on to a string you can do that as if it were like let's say an array or something like that or a vector rather in rust and we'll get two arrays and vectors in a little bit now as far as creating a string i mean we can do like you know let hello equals hello like that and this is this type okay so it's a mutable fixed length if we want to create this string that's growable heap-allocated data structure then we want to do string and that we want double colon from and then we put in and put in our parenthesis in whatever we want as the string okay so i can go down here and we can print line and let's just print hello and if we clear this up and run it we get hello all right now if we want to get the length say get get length we can use the Len method which will actually work for either type you know primitive string or this type of string so let's do print line and let's say length and then we can do hello dot Len okay so if we run that we get length five now using this type of string we can actually add on to it now there's two methods that there's a push method and is a push STR push string the push is for the char type which remember is just a single Unicode character so if we wanted to we could do hello dot push and let's say we wanted to push a Unicode character I'm actually gonna put a space hello space here and then let's put a W like that now it's giving me this error here because it says cannot borrow hello as mutable as it's not declared is mutable so we just simply have to go up here and say that this is a mutable variable okay so now that should work if we run it we get hello w now if I try to put an O in here automatically we're gonna get an error it says character literal may only contain one code point because this push method is four characters now if you want to push more than that then you want to use push string or push STR so let's do hello dot push underscore STR okay and then we want to put in our double quotes and let's do o our LD exclamation so now if we run that we get hello world alright so we'll say this will push on a char and this will push a string now we wouldn't be able to use these if we just do this like if we set this to just hello like that and we try to run it it's not going to work okay because it's it's this type so let's just put that back and let's take a look at some of the other things we can do we can get the capacity which is basically the the number of bytes that it can store so let's say capacity in bytes so let's do a print and we'll say capacity hello dot capacity so if we run that we get capacity is 12 now we also have is empty if we want to check to see if a string is empty so we'll do print line let's is empty hello dot is underscore empty and there's a ton of methods I'm obviously not going to go through them all I just want to show you a couple of them so here we get is empty and that's false so I'm just going to put a comment here we'll say check if empty now we can also check to see if it contains some sub string using contains so let's do a print and we'll say does it contain the word world so over here we'll say hello dot contains in then we'll pass in world okay so this will give us a true or false value so let's run it and we get contains world true now if we go ahead and take off this D here and we run it now we get contains world false okay so we can check to see if it contains a substring we can also replace and there's really good documentation for rust if you want to check that out if you want to look into more of these let's do place so over here I'm just going to do hello dot replace and let's say we want to replace the word world and then we put in a second parameter let's replace it with there so have it say hello there instead so we'll go down here we'll run it and now you can see for replace we have hello there now we can also loop through strings like let's say we want to loop through by whitespace so each word will be put on a new line or we can do whatever we want with it so I'm gonna say loop through string by whitespace so we can use a for loop I'm just gonna say word you can use whatever you want here a lot of times you'll see token but we're gonna say forward in hello dot and then we can use split underscore whitespace so we can split it by the whitespace and then let's just do a print line and let's just I'll put the word whoops word not world yeah we'll just do that I guess that's fine all right put our semicolon in and then let's run it and we just get hello on one line and then world okay and the more words we put obviously those would be put on separate lines let's see we can also create a string with a certain capacity so let's say create string with capacity and a lot of this stuff might seem weird like when will I ever use this but remember rust is a it's it's used for systems programming so you do a lot of different things than you would with like let's say JavaScript so that that's why it has some of these weird rules and some of these just stuff that you're not really used to in languages like JavaScript so let's go ahead and just create a mutator bol string s and we'll set it to a string and we're gonna say with capacity okay and then let's just make the capacity ten now I'm gonna take that string and just push on to it a character so use single quotes here so a character literal is just one unicode character let's do a and then let's do B and then if we were to print this out let's just put in here s okay so if we run that we should just get a B which you can see right here I'm just going to get rid of this last print so we get a B now I actually want to show you how to write assertions and this doesn't have to just do with strings this is just if you want to test to see if something is equal to something else basically is the left equal to the right so it's a assertion testing and the method that we're going to use here is assert right here start underscore equal or EQ exclamation and it just takes in two parameters and it matches the left to the right so let's say we want to make sure that two is the value for the length of the string so s dot le n okay now if I save this and we run it nothing happens because it passed okay it's only going to show us an error if it fails so let's go ahead and change this to three and go ahead and run that and the assertion fails so it says three thread main panicked at assertion failed left equals right left is three right is two okay and it tells us where it failed so this is something that is very helpful that you'll probably use quite a bit in Russ programming so let's say let's put that back to two and then I'm gonna copy it down and let's say let's match 10 to the capacity because remember we set the string capacity to 10 so say capacity whoops do that and if we save that and run it okay nothing happens if we change the capacity let's say eleven and we run it then the assertion fails okay so this is very helpful if you want to check to see if something is equal to something else all right so that's I'm just gonna stop here for strings there's a lot more methods that you can you know look in the documentation and check out but we're gonna move on to tuples so I'm gonna create a new file here called tuples dots not J SRS and let's do our public function run and we'll go ahead and bring tuples in here so I'm gonna comment that out and bring in tuples and then down here say two bulls all right now a tuple is pretty simple it's basically just a group of values so I'm just gonna paste this in real quick so they're tuples grouped together values and they can be different types so they don't all have to be the same types like an array max twelve elements so that's the most you can have in a tuple so for instance let's create a tuple called person now we need to add the types first so we're gonna put colon and then parentheses and I'm gonna I'm just going to do my name location and age as a value so first one is gonna be a string literal which is gonna be ampere Sam STR so it's two next one you never say STR and then let's do make the last one an 8-bit number which can be 0 to 255 okay and then we're gonna set equals to parentheses and then we put the actual values so let's say Brad mass and then 37 okay so to access these let's do a print and let's do Brad is from mass and is then a placeholder now to access let's say the first value which is Brad we're gonna put in person dot okay we want to use this dot syntax and you can see in vs code it knows that the person tuple has three values so we want the first one which is zero then we'll do person dot one and then we'll do person dot two okay so if we save that and we run our main file we get Brad as for mass and it's 37 okay so that's that's pretty much it tuples are pretty simple to understand so the next thing i want to look at is arrays so we'll go ahead and save that and let's create a new file called arrays dot RS put our public run function and let's bring in arrays comment that out alright so save that now arrays are actually fixed the length is fixed unlike some other languages so fix lists where elements are the same datatypes it's another thing we do have vectors which I'm gonna go over next which are grow a bowl arrays basically you can add to them so let's go ahead and create an array so I'm going to say let I'm just gonna call this numbers and I'm gonna set the data type here to int 32 and I'm gonna put a semicolon and then the the length of the array which is going to be 5 okay and then we're gonna set some brackets and let's do 1 2 3 4 5 ok so we have an array of 5 elements let's see why is okay so this green line just means that it's not being used now what I'm gonna do is just print this out so it's a print line now if we want to print out the whole array we're actually going to use the debug trait here so it's gonna be an exclamation and then a question mark and then let's just put in numbers okay so I should print out the whole thing so let's go down here let's run our file and it shows that we have 1 2 3 4 5 now if I take out the last one here and I run this it's giving us an error because as you can see it expected an array with a fixed size of 5 elements and it found 4 so it has to be exact also if I were to change this to let's say a string and run this we get expected in 32 bit int ok so the data type and the length have to be exact now to get a single value we can just let's copy this down so let's say get single Val and I'm just gonna get rid of this and let's say we want to get the first one so it's zero index just like any other array in any other language so we'll save that actually let's go right here and let's just say single value okay we'll run that and we get single value one okay so similar to just about every other language now if we want to add on to this I'm sorry not add on if we want to change one of these we can do that we can make this mutate able by just putting mu T here and then let's do let's say we assign of value okay with a raise we can't add on to them but we can change values so if I say numbers and let's grab the two index and let's set it to I don't know 20 okay if we do that and we go ahead and run now we get one two and then the two index which was three is now 20 okay so we can't we can do that with arrays we can get the length get array length so if we do a print line and let's just say array length we can simply do numbers dot Len okay so we'll run that and you can see we get five we can also get the amount of memory that it takes up so I'm gonna say arrays are stack allocated allocated now with the standard library we have something called mem so we can do actually let's do a print and I'm gonna say the this array occupies whoops placeholder bytes and then here let's do STD double colon mem and then we can do another double colon and we can get the size of Val alright and then we want to pass in a reference to the array of numbers so we want to do am per sample numbers all right and then let's run that why sometimes the errors take a minute to clear and it's confusing so we get array occupies 20 bytes okay if I get rid of let's let's change this to 4 and get rid of one of these and save it and then we run this again and we get array occupy 16 bytes okay so basically each one of these is gonna be 4 bytes so that now this STD the standard library we brought in mem if we want we can bring that in up here we can say use STD mem and then down here we can simply say mem and get rid of the STD which is always a good thing to get rid of STDs so let's run that and we get the same thing okay so this is this is pretty common to bring it in up here and then just use it now another thing we can do is we can get slices from an array so let's say get slice so I'm just going to create a mutable mutable variable actually it doesn't need to be mutable we'll just call this slice and we want to set the type here and we're going to use the ampersand and then brackets and say this is int 32 and then set it to a reference to the array of numbers so ampersand numbers and we can we can to get the whole thing in fact I'll do that first and then we'll just print I'll say slice colon and then over here we'll just pass in slice like that and let's see what's going on here Oh since this is since it's an array it's going to print out we have to use the debug trade here wait why oh there we go okay so slice and you can see we get the whole thing now let's say we just want to get 0 to 2 we could put brackets here we can actually do 0 dot dot - okay so let's see what that gives us so for now for the slice we just get 0 we just get the first 2 basically okay so you can kind of slice things out if we wanted to get let's say 1 2 3 we could do that and then we get 2 and 20 so that's a raise now I want to take a look at vectors next which are arrays that you can actually add to and remove stuff from so let's create a new file I'll call this vectors dot RS and you're probably going to use vectors more than a race I'm gonna copy this though I'm going to copy the whole erase file and paste it in and just replace some stuff here so vectors our resizable arrays and to define a vector we can do it basically the same way except instead of doing this we define it as Veck and then inside angle brackets we put the data type which will be I 32 and then right before the brackets here we want to put Veck exclamation that will define it as a vector now we can still reassign values like this we'll go ahead and print it out we can get single values it's going to be the same thing down here the array length I'm just going to change this to vector length I'll say get vector link and vector alright so let's actually save that and let's go to main dot RS and let's bring in vectors go down here okay and then we'll clear this up and we'll run this and you'll see we'll get the same stuff so we're printing out the the entire vector we can get a single value and get the length get the number of space a number of bytes it occupies slice and so on now if we want to add to this we can use push okay so I'm gonna go I'll go right here and let's say add on to vector so we'll say numbers dot push and let's add on five actually we'll add another one let's do six okay so we'll save that and if we run it now we get one to twenty because we did the reassignment four five six okay so we can add these on we can also pop off the last value with the pop method so if we say numbers dot pop and we go ahead and run that now the six should be gone so now we just get one through five okay so we can add and remove from vectors now of course we can also loop through these I know we haven't gotten two loops yet but I just want to show you this real quick so let's do loop through vector values so we're going to use a for loop and let's just say for X we're going to use in so this is a four in and we're going to take numbers and we want to do dot it err so dot iteration and inside here let's just do a print line and we'll just do number and then over here we'll just put okay so that should loop through them all and print them out so let's run it and then it just prints out one it just prints out the whole vector okay with the word number before it now we can also mutate each value so let's say loop and mutate values so basically we can do like 4x in numbers and we're gonna do inter underscore mute or mut and we can do whatever we want let's say we want to multiply each one by two so we do an asterisk X and let's do asterisk equals two all right and then what I want to do is just print the whole thing out down here so you can see what it gives us so say numbers Veck and let's put in here numbers all right so run that and now you get an error here cannot be formatted with the default format or I keep forgetting if we're gonna print out the whole array we need to do that so now at the bottom you'll see numbers vac and we get to 448 and 10 so each value has been multiplied by 2 and for those of you guys that are JavaScript developers this is similar to how map works really when you use an array and then you use the dot map high order array method so you can basically return an array that has been mutated in some way alright so I want to move on to conditionals so let's create a new file conditionals dot RS and we'll create our public function run all right so a conditional is used to connect the condition of something and then act on the result so it's just like any other if-else in any other language let's go ahead and bring in that file and down here I'll go ahead and run conditionals run all right now I'm going to create a variable let's say let age and we'll set this to let's do 18 ok and then I'm just gonna do an if-else so we'll say if age is greater than or equal to 21 then let's do a print oops let's do a print line and all we're gonna do here is I'll put a string and we're gonna say that this is from the bartender so we'll say bartender says what would you like to drink so pretend we're making like a silly little game here or something okay so if the age is greater than or equal to 21 the bartender asked what we want to drink so if we go ahead and run this we get nothing because the age is 18 so if we change it to 22 and we run it what would you like to drink now we obviously want to handle if the person is not 21 if they're if they're you know younger than 21 so we're just gonna put an else and I'll just go ahead and copy that and instead of asking what we want to drink we'll say sorry you have to leave okay so now we'll save that let's change the age back to 18 and run it and we get sorry you have to leave so let's add in let's look at operator so we have + or I'm gonna put a an extra variable here of check ID okay and we'll set that to false okay and we can add types here if we want statically so we can say this is gonna be a boolean and this is gonna be let's say an unsigned int 8-bit all right now down here we want to make sure that the bartender has checked the IID okay so let's do and so we can do double ampersand check underscore ID and then I'm going to change this one to an else if and we're gonna say if age is less than 21 and check ID because the bartender should have checked the idea already else then let's do a print so grab that and but the bartender is gonna do is ask for the ID so we'll say I'll need to see your ID alright so we'll save that and let's run it so we get I'll need to see your ID because this is false so let's say they check the ID however they're 18 so sorry you have to leave so if they check the ID and they're 30 then we get what would you like to drink now we can also do or okay so here we did and what I'm gonna do is create another variable and I'm gonna say nose person of age so the bartender knows this person and they know that they're of age okay someone that comes in all the time so right here for this condition I'm gonna add in an or which is represented with a double pipe character and will say nose person of a okay if this is true then the bartender knows them and they know they're of age so let's run that and we get what would you like to drink now even if we set the age to 18 if we run it what would you like to drink because since this is true this this doesn't apply and this is kind of a silly example but it's just to show you the syntax basically it's just a regular if we don't have to put parentheses around this we do use curly braces just it's very simple so I also want to show you how to do a shorthand and now there's no ternary operator in rust as far as using you know the question mark and the colon like in many other languages but we can do a shorthand if so let's go down here and let's do a shorthand if so I'm gonna set a variable and I'm gonna say is of age and we'll set that to if age is greater than or equal to 21 then set that to true else set to false okay and then we'll go ahead and we'll just print line and pass in is of age X let's put some text here say is of age all right so if we save that we run it we get is of age false so it's looking at this value here now if I change this to 22 and then we go ahead and run this we get is of age true okay so this is how we can do a shorthand if that's similar to how a ternary operator works all right so those are conditionals now we're going to move to loops okay so I'm going to create a new file called loops running out of time here so I'm going to try to go kind of quick inside main RS let's bring in loops loop's and then down here let's comment that out loops run ok so public function run now there's there's different types of loops just like with many other languages I'm just going to paste this text up here so loops are used to iterate until a condition is met in rust we have what's called an infinite loop so I'm going to show you that first I'm going to create a mutable variable called count we'll set that to zero and then we can simply say loop okay so this is infinite loop and we're going to just take that count variable and we're going to plus equals 1 so we're going to add 1 to it through every iteration and then let's do a print line and we'll just put or print the word the word number and then we'll put in the count okay now this will just keep going forever if I run it like this so we just need to put a condition here we'll say if the count is let's say equal to 20 then we want to break okay we want to break out of the loop so we'll save that and run and you'll see that we'll get 1 through 20 number 1 through 20 so it's up to us when you know to write a condition down here to stop the loop otherwise it'll just keep going so next type of loop I'm going to show you is a while loop which is very common in many languages and I'm going to just I'm just going to do a quick fizzbuzz ok so if you don't know what fizzbuzz is it's a pretty popular challenge they use it in coding interviews and stuff basically you want to loop through 0 to 100 if the number is divisible by 3 you want to print out fizz if it's divisible by buzz you have if it's divisible by five you want to print out buzz and if it's divisible by both you want to print out fizzbuzz otherwise just print the number all right so let's do while the count is less than or equal to 100 okay and then we want to put us put some conditionals now remember it needs to print out fizzbuzz if it's divisible by five and three now a shorthand to do that is to just see if it's divisible by 15 so we use the modulus operator the percent sign so if it's divisible by 15 or actually if this is equal to zero that means it's divisible by 15 because the modulus gives us the remainder and then we're gonna print line and let's just do save fizzbuzz okay and then let's do an else if and we want to check if the count modulus 3 is equal to zero okay if that's true then we want to print line and let's just print out Fizz since it's divisible by 3 okay and then we'll do another else/if and we're going to test to see if the count modulus five is equal to zero and if that's the case then we're going to print line and we want buzz okay else then we simply want to print out the number so we'll just say print line and let's put in the count okay now we need to increment it down at the bottom so say increment so count plus equals one all right so that should work I'm actually gonna comment out this loop though just so that doesn't run and let's see if I pass the challenge so we'll run it and I didn't and that's because it Wanda too much JavaScript so you want to use the double equals okay so if we go up to where this started which is right here so we get one two fizz so remember if it's divisible by 3 we get fizz four five is buzz six is fizz seven eight nine is fizz 10 is buzz fifteen should be the first fizzbuzz okay if we keep going 30 should be the next one 45 should be the next one then 60 so it looks like looks like it's working so that's that's a while loop now we also have a for range loop so it's a for range so I'm going to do 4x in and I can say zero dot dot to a hundred okay was just kind of cool and I'll do the same thing here so we'll say if actually you know what I'll do is just copy that and put that in here except instead of count let's do X okay so we'll do the same thing fizzbuzz and will comment out this while loop okay so basically it's just for whatever you are iteration variable is in and then you can put a range like this okay and we're just doing the same thing the same physical challenge so let's go ahead and run it okay so if we go up top here this is just telling us that we didn't you we're not using the count variable that we set it's just a warning so one two fizz for buzz fizz fifteen should be fizzbuzz good so there we go we have an infinite loop we have a while loop we have a four range so we're gonna go ahead and move on so next we're gonna look at functions which we have been working with obviously but we're gonna look at them a little more so let's create a file called functions dot RS and in our main file let's bring in functions and run functions run okay so a function is just used to store blocks of code for reuse just like any other language so let's first create our run function so FN run and then outside of that I'm gonna create another function so let's create one called greeting this is just gonna be a simple example so we'll say function greeting and this is gonna take in some parameters so I wanted to take in a greet like hello hi something like that and then a name so let's do greet and then we're gonna set the type of this to be a string so we want to do ampersand STR and then let's also do a name which will also be a string okay now all I want this to do is print so we're just gonna print a line here and let's just do two of these and then we'll say what do we want to do here actually it's yeah well just do that so hello name and then we'll just say like nice to meet you and then here we're gonna put the parameters so greet and name so very simple and then up here we can run the function by simply doing greeting and we'll pass in hello is the greet and let's say Jane as the name so if we run that we get hello Jane nice to meet you so very easy next thing I want to do is another one to just add a couple numbers so let's do function and for this one I don't want to just print I want to return from it so let's do function add and it's gonna take in let's say n1 so num1 which is going to be a 32-bit integer and then it's going to take in num2 which is also i 32 and then we want to return in i thirty two so we use an arrow syntax like this okay and we set this to I thirty two because that's the datatype that it should return now to return we can just do n1 + and - and we don't use a semicolon okay so when we don't use a semicolon that is telling it that this is what we want to return now up here in run we could we could print it if we want directly the function directly but we can also bind function values to variables so we can do like say let's get underscore some and we'll set that to the add function and we'll pass in five and five and then we'll go ahead and print the sum and we'll set that to get some okay so we'll go ahead and run that and now we get some ten okay so if you want to return something you can simply do this just no semicolon now we can also do closures which are pretty handy they're nice and compact you can also use outside variables so within the run function right here let's say closure and I'm gonna do let add nums and what we want to do is set whoops slashes we want pipe characters and we're gonna say n1 which is going to be an i-32 and we're gonna put comma and then n2 which will also be an i-32 and then on the other side of the the pipe character here we're going to return n 1 plus n 2 plus n 2 okay and then we're gonna do a print line and let's just say see some for closure some and over here we'll say add nums and we'll pass in let's say three and three and let's save that okay so if I run that we should get see some six now what's cool about this is we can use outside variables which we can't do with a standard function because it's block scoped so if I do let's say let and three which I'm gonna set to be an i-32 which it is by default but I'm still going to set it and then I'll set that to let's say 10 now I can go down here and I can just add it in here so I can say plus + 3 now when we run it we should get 16 okay so we get see some 16 okay so pretty cool nice and compact also we can use outside variables so those are functions next thing we're going to look at our pointers so our pointers and references or point to references so let's create a file here called pointer underscore ref dot RS okay and we just paste this in so reference pointers they basically point to a resource in memory I'm just gonna bring this in here as well so let's see pointers pointer ref and then let's do pointer ref run okay and then we'll create our run function so it's a pub function run now this is basically if we have a primitive array we can create a variable in point to another variable okay not just an array but any primitive value so I'm going to give an example this is a primitive primitive array so we'll say let array one equals one two three and then let's do let array two equals array one all right now I'm gonna go down here and I'm gonna print line and let's put in our colon question mark because I want to print out the whole array or let's just say values and then we'll do let's do array actually I want to print out both so I'm going to put that and let's do a ray one and array two and let's see what that gives us so clear this up and we'll run it so we get one two three and one two three we were able to create this array and then set array to two array one now let's try the same thing with vectors which are non primitive okay so I'm gonna paste this in so this is actually from the documentation so with non primitive value with non primitives if you assign another variable to a piece of data the first variable will no longer hold that value you're gonna need to use a reference which is the ampersand to point to the resource okay so let's actually grab this and let's say vector because a vector is not a primitive value and we're just gonna set this these two vector set a vector we use VEC exclamation before the brackets okay now down here I'm just gonna let's change this to vac like that so I'm gonna try to run this and you'll see we already have an error it says use of moved value Veck one value used hereafter move so basically once we set vac one our Veck two equal to vacuum this is no longer applies and if I run this we're gonna get that same error so what we need to do is use is make this a reference okay so we're basically pointing to a reference and we do that with an ampersand okay and we'll also have to put the ampere samp down here in front of Beck one so if I save that you'll see the error goes away and if I run it now we get one two three and one two three okay so you can't point directly to it if it's not a primitive value you have to create a reference and we've done this in other files as well use the ampersand alright so I'm not gonna go too much further into it it's that I just wanted to cover that cover the basics of how that works alright so next thing I want to look at is Struck's case trucks are very important in rust they're similar to classes kind of basically you create you know members or attributes and then you can also have functions that are related to the struck so I'm going to create a new file called Struck's dot RS okay and let's bring it into main so Struck's bring that down so let's save that let's go into strux I'm gonna paste this in so you basically used to create custom data types and clear that up and let's create our public function run so I'm gonna so I'm going to create two different structure in this file first is gonna be for a color okay an RGB color so let's do struct color okay you want to use uppercase that's the convention and then the property so we're gonna have red green and blue and I'm gonna set a type of u8 okay so an unsigned just means it's a positive value 8-bit integer is 0 to 255 which is very convenient because the RGB values are 0 to 255 so let's say green that's also u8 and then blue also u8 okay so that it's as simple as that to create a struct now I'm gonna go actually you know what we want to we want this outside of our run so we want to put this up here okay and I'm just going to say that this is a traditional struct because we also have a tuple struct which I'm going to show you as well so down in the run we're going to create a new color so let's create a variable immutable variable called C and set that to color okay the color struct which takes in a red value which I'm going to say is going to be 255 a green value which will be 0 and a blue value which will be 0 so it's red basically and then let's go ahead and do a print and I'm gonna say color and then we want there I want to show all three values so the way that we access these is is with the dot syntax so we can just say C dot read you can say C dot green and C dot blue okay so we'll save that and why is this giving me an error here expected one of oh I forgot my semicolon here okay so we're gonna run this now it's just giving me this because this doesn't have to be mutable but it's gonna need to be mutable in a second so I'm going to ignore that and you can see we get color 255 which is the red 0 and then 0 all right now we can change these properties are these members if we want by just simply doing C dot and then let's say I want to change the red value to 200 okay so if I save that and run it now we don't get that error and you can see red is now 200 so we can directly change these these properties right here now we can also create what's called a tuple struct I'm going to use the same kind of example with the color so let's go let's say tuple struct and I'm gonna set struct color and then it's just where all we're going to put in here is the data types which will be you eight so three unsigned 8-bit integers we should probably comment this out and we're gonna get some trouble same thing here okay so that's our struct now down here I'm going to create a new variable so same thing we'll just do C let's do let mute C and let's set it to color and then we can just pass in our values so let's do 255 0 and 0 and then I can do the same thing i can print each one so I'm just going to put in color and three of these now in this case we didn't name red green and blue we just kind of passed in the the values and the datatype so we would do C dot zero for the first one C dot one and C dot 2 okay so just go by the index and then if I save this and we run it we still we get color two five five zero zero we can do the same thing we can change values okay so instead of doing C dot red I'm going to do C dot zero which will grab the first one of 255 and change it to two hundred so now if I run it we get color two hundred zero zero okay so that's these are pretty simple examples now what I want to do is is build something a little struck that has functions associated with it so let's let's actually comment this stuff out I know it gets a little confusing with all the commented out stuff but I want you guys to have it as a reference so we're going to go up top here and we're going to create a person struct okay so we're gonna say struct person and person is just going to have a first name which will be a string and a last name last name string okay now I want to create some functions that are associated with the person struct so I'm gonna go right here I'm gonna say implements or impl the person struct and then we can put functions in here and the first one is going to be just to construct a person so to create a new person so it'll be a function I'm gonna call it new and it's gonna take in first which will call which will be a string so STR and then it's going to take in last which will also be a string and it's going to return a person and then we're just going to simply say look we want this to be uppercase so gonna take person and we're going to assign first name to first that's passed in now since we use string like this this upper case s string we actually have to tack on dot to underscore string like that all right we're gonna do the same thing with the last name so set last name that's gonna be last to whatever's passed in and then we're gonna set that to string like that okay so that's our new function now let's actually use this so down in the run I'm just gonna go below all this stuff and let's say let mutate able variable we'll call it P will set it to person : : new because we're calling that new function and let's pass in the first name of John and a last name of Doe all right so now let's do a print oops let's do a print line and we'll say person and let's do two of these placeholders and then for the first one we're gonna do P dot first name and then P dot last name alright let's save that and let's run it okay so we get person John Doe we just get this warning because this doesn't have to be mutable just yet so let's say that we want to create a method that just gets the full name so I'm gonna go under the new function that we created and let's say get full name and we'll create a function called full name and this is gonna take in amperes amps self so self is similar to how this works and many other object-oriented languages you are just basically referencing the the struct of person so you can kind of replace self with person and then this function is basically going to return a string so we want to do that and then we're going to use format here format exclamation because it's a macro and it's similar to print line except it doesn't actually print so in here we can use the same type of formatting so we're just going to put two curly braces and the first one we want to replace with the first name so self dot first name and then self dot last name like that let's see why is that oh we don't want the semicolon because we're actually returning this okay so that's the full name now if we go down here where we created the person we should now be able to get the full name so let's do another print line actually know what we'll just replace this so we'll say person and then let's just have one placeholder and let's replace this with P dot full name which is a method so you want to make sure you have your parentheses okay so now if we run this we should still get person John Doe instead of calling both the first name and last name property though it's calling the full name method now we can also mutate stuff within the struct so these properties let's say we want to change the last name so let's say maybe the person maybe it's a woman that gets married or something like that or a man that gets married I don't know whatever who knows these days so let's do set last name and we'll do function set last name okay now since we're gonna change something here we're gonna do an percent mute so permutate self and then we're gonna pass in let's just say last which is going to be a string okay and then we're gonna say self dot last name and we want to set that to last that's passed in but we want to add dot to underscore string like that okay so I believe that should work so now let's go down here and we created let's do Mary Doe and then I'm gonna take that person object and let's say set last name and then in here let's change it to Williams okay and will actually take where we printed the full name here and let's let's take one of these and let's move it up here so I want it to I want to create Mary dough and then show the full name and then set the last name and then show the full name again so we should get married oh and then Mary Williams so let's run it and there we go okay so we can change these properties as well let's create one more method up here and let's let's show the name as a tuple or set it as a tuple so we'll say name to tuple so we'll say function let's call it to tuple and pass in self and we're gonna get a tuple with two strings first name and last name that's what it's going to return here all right and then we want to return so no semicolon and we want self dot first name and self dot name okay no semicolon because we're actually returning this so now we'll go down here and let's do I will just say just bring this down and let's do P dot 2-tuple and I'll just say person to pool okay so we'll run that let's see what do we got here missing a parenthesis P dot 2-tuple doesn't implement display Oh since it's a tuple we have to actually use the debug trait here okay so let's run that and now down here you see person tuple we have Mary Williams as a tuple alright so hopefully that kind of gives you an idea of how structs are used they're similar to classes in you know for instance Python and PHP JavaScript Java okay but we're gonna go ahead and move on here so I'm gonna close that up and we're gonna talk about enums now so I'm gonna create a new file called enums dot RS and then let's create or let's point to that file so go ahead and bring that into our main RS so mod e nums and then down here let's do a number on okay so in here I'm just gonna paste this in so enums are types which have a few definite values which is very vague but I'll show you what I mean so we're going to first of all create our run function here so let's say pub function run and up above it I'm going to create an enum type for movement okay so let's just pretend that this is some kind of game where we have an avatar and or avatars and we want them to move in certain directions so in here we have what I call variants so we're gonna have up/down comma here so up down left and right okay so these are our variants for our enum and then I'm going to create a function outside of the run called move underscore avatar and this is going to take in our movement enum as as a parameter so we'll say m we'll set the type to movement now in here we want to perform an action depending on the movement okay so we're actually going to use let's say perform action depending on info and we're going to use something called match which is similar to a switch if you've ever used a switch which is a type of conditional so we're gonna say match for the movement and let's say if the movement so movement and we want to use double colons and you can see we have now we have this drop down so let's say if it's up we want to use this fat arrow right here we want to say print line we could do whatever we want but I'm just going to print something out on the screen so I'm just going to print out avatar moving up okay so pretty simple and let's copy this down four times website put the comma on the inside we don't want that okay so we'll do up down left right ba ba start those of you that I've that are old enough know what I'm talking about you played Nintendo so let's say down and then we have movement left and right okay so that's our match now down in the run what I want to do is set a couple variables so let's do let avatar one and let's say we want him to move left so we'll say movement movement left like that and then I'm gonna have a couple more here let's have one move up right and down all right we'll just change the names for these variables we'll do two three and four okay so now we want to call the Move avatar function and then pass in each avatar so let's say move avatar and this takes in a variable let's say avatar one copy this down to three and four and depending on what we put here is which way they're going to move so let's go ahead and save this and let's see I already brought that in here so let's run it and we get avatar moving left so that would be this one right here moving up which would be avatar two moving right moving down all right so I know these are simple examples that aren't really practical but I just want the goal of this course is to get you familiar with the syntax you know so you can come back and you can look at how things are formatted and you can think of your own ideas you know this is definitely just an introductory level course so the last thing I want to do is look at command line arguments basically we can pass stuff in when we do cargo run we can pass something in here you could create some kind of to-do list or something if you wanted to but I'm just going to kind of show you the basics of how to get those parameters so we're gonna let's create one more file here called we'll just call it CLI for command line interface dot RS and we're gonna bring this into our main file so it's a mod CLI and then down here I'll say CEO I run okay so in CLI RS we're gonna create our run function and I'm gonna create a variable called args okay so say ogz now I want this to be a vector so let's give it the type of Veck and it's gonna be a vector of strings so we're gonna put some angle back brackets and say string now I'm gonna be using from the standard library the env okay env args which are used to get any arguments that are passed in when we run cargo run so we can do STD double colon args and then it has something called collect a method called collect like that now just to shorten this just a little bit I'm gonna go up here and say use STD env that way we can we can get rid of actually I'm sorry this should have been STD env and then args like that but now we can get rid of this and just have Hanvey alright now I want to show you what this gives us so let's um let's do a print and let's just say args just to kind of show you what this gives us and we're gonna use the colon question mark and then I'm gonna pass in here args see if that works okay so let's run just cargo run and see what that gives us so this args right here is going to give us a vector and the first element or the first value in this vector is going to be the the target of the executable okay that's always going to be the first one now if I pass something in like if I say cargo run hello and we run it take a look at that so now we get a vector has the the path and then it has the hello okay whatever we passed in if I say cargo run hello world hello and world they both get added here okay so you can see we can grab that input so I'm gonna create a variable called command and I want to get for instance if we pass in cargo run hello I want that hello to be the command so I'm gonna set this to args 1 because 0 is going to be that path so we want the 1 index and I'm gonna clone it so I'm gonna say doc clone and I'm gonna put it into that command okay so now down here let's just see what we get when we put in command get rid of that and we'll say command and we'll run that and we can see command hello all right now this should give you some some pretty good ideas on creating some kind of command line application after you see how to do this so we can get the command and then what I'm gonna do is create get rid of that I'll actually we'll just comment it out we're gonna do an if statement here we're going to say if command is equal to hello then let's do a print line and let's say let's actually put another variable up here with the name and we'll set the name to Brad or whatever you want to set it to and we'll go down here and we'll say hi put the placeholder here how are you okay and then we want to put the name here so now if I save this and I go down here and I say cargo run hello we get hi Brad how are you so we can check whatever that command is and then we can do something with it okay let's do like an else if command is equal to status okay and then we'll go up here and we'll put a variable of status and I'll set that to let's say 100% okay and then we'll go down here and we'll do a print and let's say status is and then we'll put status here like that it'll say cargo run status we get statuses 100% all right and then I don't know we'll just do like an else and we'll do a print and let's just say that is not a valid command okay so now if I do like cargo run hi we get that is not a valid command all right so I mean I'm not going to go much further than that I just want to kind of show you the basics of how we can grab input and then we can do something based on that input but there's there's so much more guys this is just scratching the surface I know this is a long-ass video but there's so much that you can do we didn't even get into like dealing with files and stuff like that it's rust is a great language it's it's pretty it's difficult compared to you know Python and JavaScript and PHP and some of the other languages but it's very very powerful it's very fast so definitely something to look further into like I said I do want to do something on I've assembly I think that that is gonna be it's gonna be something big in the future obviously it's still in the very early stages but you know I'll be doing something with web assembly soon so that's it guys if you stayed through this whole crash course thank you that he was very very long I know that but I did want to get all these fundamentals in the course so that's it guys thank you so much for watching and I'll see you in the next video
Info
Channel: Traversy Media
Views: 561,644
Rating: 4.869092 out of 5
Keywords: rust, rust lang, rustlang, rust programming
Id: zF34dRivLOw
Channel Id: undefined
Length: 110min 43sec (6643 seconds)
Published: Wed Feb 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.