Rust Tutorial #1 - Introduction To Rust Programming
By Tech With Tim
Summary
Topics Covered
- Rust Compiles to Standalone Binaries
- Rust Delivers C Speed Without C Pain
- Rust Powers Firefox to Machine Learning
Full Transcript
[Music] hello everybody and welcome to a brand new tutorial series on this channel where i'm going to be covering the rust programming language now i'm super excited about this series rust is an
awesome programming language i've really been enjoying it over the past few days and i think you guys are going to enjoy it as well going through this series now a big shout out to my discord server discord gg
twt can join that from the link in the description for actually recommending this video idea and kind of constantly bombarding me about rust because now i'm finally making it they have convinced me
i'm gonna go through and teach you the rust language ideally here we're going to have between 15 and 20 videos in this series i'm going to cover everything from the complete basics so setting up your environment to some more advanced
rust concepts and show you some of the power of the rust programming language with that said i will quickly mention here that this series is not designed for complete beginners i'm assuming you
have some familiarity with programming you've touched programming languages before or at least one before otherwise it will be a little hard to follow along here however if you are a beginner
programmer that's fine i'm just assuming you kind of know what a variable is you understand what a command prompt in a terminal is i'm not going to explain all of the super basic stuff that i imagine most of you are familiar with now with
that said if you are a complete beginner programmer or you're kind of in the beginner stages and you're looking to get better at programming you can check out my course programming expert dot io you can use discount code tim that is
literally the best resource it's interactive and it will teach you how to get up to a software engineering level in programming really as fast as possible so check that out from the link in the description and with that said
let's get into the rust language here i'm going to give you a quick intro to the language show you how to set up your environment for mac linux and windows then we'll write and compile our first rust program so as i was saying i'm just
going to give you a quick intro here to rust spend a minute or two talking about some of the benefits and features of the language then we'll get into the installation so rust is a statically and strongly typed programming language it's
also compiled and what that means is that if you write a rust program you compile it into a binary executable file and that file can be ran by anyone who doesn't actually have rust installed so
this differs from something like python or javascript or ruby where you actually need a python interpreter for example on your system to be able to run the code this is different someone does not have
to have rust installed to run your rust program assuming they're on the same operating system that you are on when you compiled the program continuing here rust is known as a very fast and
reliable programming language it's very good at error handling and catching bugs and a lot of developers use it because it is very efficient like it's a very fast language in terms of execution
speed but it also is not extremely difficult to write in the main benefit of rust is that it has a bunch of high level features but it also gives you control of lower level features and code
without kind of the difficulty of languages that typically do that like c or c plus plus so really that is why you would use rust you want to make an efficient scalable very fast system of
some sort but you don't want to deal with kind of the annoyance of writing code for example in c yet you still want to have the control of those low level features again this is a super high level overview of the language there's a
ton of other awesome features and i have a little list here i'm going to read off of some of the things you can make with rust so rust allows you to make command line tools web services devops tools embedded
devices or embedded systems cryptocurrency related stuff bioinformatics search engines machine learning and it's actually used in part of the firefox browser if you want to learn more about
this language i will link the rust programming language book in the description this is completely free this is the official kind of rust documentation here for getting started and learning some concepts and really
what i'm going to be doing in this series is following along with this book and kind of just picking out the more important information and giving you a almost summary or overview of the important topics without getting into
all of the super kind of nuanced details that you don't really need to know to quickly pick up the language so if you want kind of a text-based resource here to follow along with go to this book again everything i'm teaching is going to be pretty much directly from here
although i'll come up with my own examples and kind of add some more detail to this so with that said that is rust again a great programming language relatively easy to learn at least from
my perspective with that said let's get into the setup and start writing some rust code all right so now we're going to move on to installation now there is different steps here for windows mac and linux so just go to the appropriate time
stamp in the video player for which operating system uh reflects yours now i'm going to show you windows first so to install rust on windows first of all you can reference this page right here
this has all the installation instructions in case for some reason what i'm showing you doesn't work although it should uh anyways what we need to do on windows is go to this page right here i'll leave this direct link
in the description and we need to download the graphical installer for rust so we can download rust knit.exe
we're going to use the 64-bit version unless for some reason you're on a 32-bit machine although i imagine most of you are on 64-bit so just click on 64 if you're unsure anyways you're going to download that uh that is going to give
you a program that you can just double click on wherever you download it to so i'm just going to load it up i already do have rust installed but just so you guys can see how to go through this and all you have to do here is hit one so you're gonna proceed with the
installation just the default you don't need to mess with anything and then hit enter now i already have this installed so it went very fast uh but for you guys it will install rust and then you should be good to go now there is one more
thing on windows however you do need to install and this is going to be the microsoft c plus plus build tools so i'll leave this link in the description go here download the build tools this is
the 2019 build tools which is what we want and then when you go through this installer here mine probably looks a little bit different than yours so i'm not actually going to run it but as it says here in
the installation documentation you need to make sure that you select c plus plus build tools and the windows 10 sdk as well as the english language pack
now i think by default it will install these for you at least that's what it did for me but if for some reason you're having some options you need to select and uh or select from just make sure you select these ones right here so c plus
plus build tools windows 10 sdk and the english language pack after that you should be good to go and if you want to test if your installation is working open up your command prompt on windows and just type in rust c and if you get
some output from this command then you're all good to go and we can move on to the next step now let me show you the installation on mac alright so i'm now on my mac computer i'm just going to quickly run you through the steps this
will be the same on linux as well by the way so i'm on the page for rust installation notice now it's not giving me the windows download buttons because i'm on mac and all i'm going to do here is just copy this now this link will be
in the description so if you want to find this command just click this link and what we're going to you're going to do here sorry is open up a terminal window and we're just going to paste this in so i believe i can use command v
to paste that if that doesn't work for you then try right clicking on your mouse and it should paste that into your terminal now what you can do here is hit enter and this should actually download the installer for you and run through
the rust setup so here we're getting the same thing we got in the windows installation all we need to do is hit one to proceed with the default installation and then hit enter and it should install rust for us now we do
also need a c compiler here to be able to run rust on mac however you should have one installed by default and once this is finished i'll show you how we can test the installation to make sure it's working all right so as you can see
here rust is now installed however before we can get rust to work we do need to restart our terminal window so i'm just going to close the terminal here i'm just going to open up a new window so let's go new window here and
inside of here i'm simply going to type the command rust c now if this command works for you then you have successfully installed rust and you're good to continue on with the video if this did not work then please check the
installation documentation again it will be in the description there may be an additional step you need to follow or you may have not actually ran this command correctly with that said that's all that i have for you for the installation now let's actually write
some rust code alright so i'm back on my windows machine and i think as i mentioned i'm going to be using sublime text here as my code editor you can use whatever you'd like but this one is free and what i'll be using so you can download this from the link in the
description if you want and i have it open here now to start writing our first rust program i'm just going to go here to file new file let me just close this one here and i'm just going to save this
file as something that ends in dot rs for now i'm going to go with main.rs and
rs is the rust file extension so you need dot rs uh you can call this whatever you want but don't have any spaces in the name and if you wanted to have a space then put an underscore so if you want to do like hello
world rather than not do like hello underscore world that's the convention for naming so for now though let's just call this main.rs and now we have a rust file now you should automatically get
syntax highlighting for this if you're in vs code you may need to install the rust extension but let's write our first rust program so to do this i'm simply going to say fn standing for function
i'm going to write the main function i'm going to put a set of parentheses a set of curly braces like this or brackets whatever you call them and then i'm going to write print ln exclamation
point i'm going to put a set of parentheses and inside of here i'm going to put a string that i want to print out now of course we need to do hello world that's what we do for our first programs every time and let me save this file and
i've now written my first rus program so i'll quickly break this down for you then i'll show you how we actually run this so fn stands for function we'll get into functions later but the main function is a special function in rust
that's going to be executed automatically when you run your rust program so you need to make sure you call this function main it's kind of the entry point of your code and anything inside of here will run
immediately now in rust you're supposed to have i believe it is four spaces not tabs for indentation although let me check on that so rust does actually recommend that you have four spaces as
opposed to tabs for your indentation level it doesn't matter so long as you're inside of the correct set of parenthesis i'm actually just going to use tabs because they're easier but just wanted to mention in case any of you are
wondering four spaces rather than taps continuing here we have print ln now this stands for print line and then we put an exclamation point now the exclamation point actually means that we're running something called a macro
i'm not going to get to that for a while but this is different than a function this is actually a rust macro and this is going to take one string argument here that allows us to print this out to the screen so that's really all we're
doing we're printing the string hello world to the screen and this is a string because it's surrounded in double quotation marks okay so let me save the program here now that we have our first program we need to know how to run this
now the steps will be very similar on mac or windows same with linux we just need to open up a terminal or a command prompt all right so now that you have a terminal or command prompt open what you
need to do is get into the directory where your rust code is so first you need to know where you saved it i imagine you guys can figure that out but mine is actually in a folder called rust
tutorial which is on my desktop so from my command prompt here i'm going to type cd standing for change directory and i'm going to change into the desktop directory then i'm going to change into the rust tutorial directory and now that
i'm here and i type ls you'll see that i see the main.rs file now all of these commands will work on any operating system so you can run them on you know windows mac linux etc now that we're
here we can actually run the russ c command which stands for rust compile followed by the name of our file which in this case is main.rs now when we do this what's going to happen is we're
going to compile what's known as our source code which is this file right here into an executable binary file which will be executable on the operating system in which we ran this
code so if i run this or sorry this command so if i run this command on windows then the executable file i get runs on windows if i do it on mac then it runs on mac if i do it on linux it
runs on linux etc and actually i think if you do it on mac it will run on linux or mac and same on linux it'll run on mac and linux anyways let's hit enter here we shouldn't get any output but
what should happen if we type ls after this is we should see that we have a main.exe file or just a file called main
main.exe file or just a file called main or main.sh or something along those
or main.sh or something along those lines if you're on mac or linux so this is actually the executable binary file and this is what we can give to people if we wanted them to run our rus code
without having rust installed so rather than giving them the source code which is in our rs file we give them the exe file and you don't need to worry about this third file right here so now how do we actually run our code well we've just
compiled the code now we have the executable file and to do this it's a bit different on each operating system but on windows it's going to be dot backslash and then main dot exe like
that when we run that command it should spit out hello world so that's what we have inside of the code here and if you're on mac or linux it's going to be dot the slash is going to be in the other direction and then i believe it's
just going to be main i don't think you're going to have a dot exe extension on there so you would just do dot slash main and you should get the same output there in your terminal okay let me just clear this with cls
again the command is russ c the name of your source code so main.rs it then gives you some type of executable file you can then run that executable file and there you go you have ran and
executed your first rust program now if you are having trouble navigating here in the terminal i will leave a video in the description that shows you some basic terminal commands you do need to be familiar with this to be able to
execute your rust code properly and i won't go through explaining all the cd commands so video in the description if you aren't able to follow along with these steps alright so that's really all that i have for you in this first video this is our
first rust program we got the installation we are now able to actually run and compile our uh what is it rust code and in case you were confused there again we need to compile first into that
executable then we could run the executable and now that we have the executable file we can give that to anyone and regardless of their environment setup well their operating system will matter but other than that they should just be able to run that
code so with that said i will wrap up the video here in the next video i'm going to show you some rust command line tools i'm going to show you how to better set up a project then we'll move on to some more rust features some more
syntax and look at all the stuff that you'd expect in a programming language if, you, guys, enjoyed, make, sure, to leave, a like subscribe to the channel don't forget to check out programmingexpert.io from the link in the description and i
will see you in the next one [Music]
Loading video analysis...