hi there serdar yagalop here for infoworld welcome to the first video of our new series
on the rust programming language rust has grown in popularity recently because of its speed
and its guarantees for memory safety both of which are highly desirable in programming languages what
i'm going to do in this video is take a simple rust program and step through its components to
give you an idea of what the language looks like and how it behaves now for the sake of
this video i'm going to assume you have at least some existing programming language experience rust
is probably not the first language you want to learn if you don't know languages at all
but it's fast becoming an important one and it's also becoming especially important for people who
already have experience with languages like c and c plus since it builds atop many of the concepts
in those languages so here is our example rust program this is a copy with some modification of
a sample program found in rust's own documentation it's a random number guessing game and since it's
offered under the mit license i have linked back to its source in the description for this video
the first thing you'll likely see in any rust program is a list of packages or external code
that are used within the program by way of the use statement this program depends on three external
packages two from the standard library and one from a third party library rand for generating
random numbers later on we'll go into a little more detail about the exact syntax for use
since the name spacing for the use statement is important next we're going to come to this
definition for our program's entry point this is the function that is first to run when the program
itself runs again if you're familiar with c or c plus this should be pretty familiar the entry
point function for those languages is named main and it's the same in rust and likewise the brace
formatted syntax for rust should be familiar from those languages as well as from java or javascript
so first we come to the keyword loop and loop and rust as you can probably guess
is a simple infinite loop construction everything inside the braces loops until we come to a break
statement that exits that loop now there are of course ways to loop until we hit a particular
condition but we can talk about that in later videos we'll look at rust's syntax in more detail
next we come to our first example of a common rust feature called a macro again macros are something
that are found in other languages too this is code that gets expanded into other code at compile time
and it has more flexibility in its definition than a plain old function definition
and you can recognize a rust macro by the use of the exclamation point at the end of
the name so print line here is a commonly used macro that's just used to print to the console
now we come to our first variable definition in rust and this gives us an opportunity to
talk about how rust handles variables in memory generally you see by default all variables in
rust are immutable they don't change over the course of the lifetime of a program
if you want a variable that does change you have to explicitly define it that way
this is part of how rust ensures memory safety by keeping mutable and immutable things separated
the let keyword precedes any variable definition and so here we're creating a variable named secret
number and we're setting it to the output of a chain of function calls in rand the
random namespace or crate we have a method named threadrng which returns a random number generator
object that's thread safe and that we use in turn to generate a random integer from one to 100.
now you'll notice that with this definition here we don't have a variable type
defined for secret number the variable type is inferred automatically in this case from the
returned object as you can see from this hint it's an unsigned 32-bit integer now we set up
another loop for getting guesses and at the head of the loop we prompt the user to make a guess
now comes another variable definition and this is for a variable that actually varies and to do this
we use a slightly different syntax we use the mute keyword ahead of our variable name to indicate our
use of this variable changes its value we then set it to a newly created blank string
next we come to a syntactical construction that's used for handling things that might
potentially fail this is where things can get a little intricate so i'm going to go slowly
first thing we're doing is we're getting the standard in object from the i o namespace which
gives us access to the standard in that we use for console input in many languages and from that we
read a line and we store the results in the guess variable notice that we're doing two things with
guess we're referring to it by way of a reference using the at symbol to store something in it
and we're indicating that it's mutable with the mute keyword now what does this expect
here well one of the things many operations return in rust is a special kind of value called a result
a result value can be one of two things the value you're looking for in this case a number
representing the number of bytes inputted or an error if we tack expect on the end of
this here as a way of running that method on the returned result this is a shorthand way of saying
if we don't get an error just proceed normally if we do get an error send that back up to the
program's error handling chain with this message now in this particular program if we do get an
error it will most likely crash the whole program because the area is not really handled it's just
sent back up to the top and there's no place else for it to go from here so the program dies
now assuming everything reads correctly we now have a string variable with the player's guess
of the number stored in it now we have to convert that that string into an integer now to do this
we can use another way of handling result objects in rust it's more verbose but it's also more
explicit first we create a variable guess num which is also an unsigned 32-bit integer and then
we take guess we clean any trailing or leading blank space from the string and then attempt to
parse it so that it can be turned into an instance of the variable we're setting it to that is we're
trying to treat it as if it's an integer and this also returns a result but we're going
to handle this result a little differently we're going to use rust's match keyword to work with it
what match does is let's let's us take something and compare it against a number of possible
options here it takes result and compares its type to one of a number of possibilities for
result we have two possibilities one is an okay value with the resulting number stored in it as
as num in this particular case or an error value if we get okay then we just return the
value that's contained in ok and we keep going if it's an error we ignore the details of the
error which is what this underscore is it's just a throwaway reference to whatever that error is
and then we use the continue keyword which takes us back up to the top of the guest loop to start
guessing again so again this is a more verbose way to handle a result but it's more flexible
we want to be able to handle error conditions for this part of the program much more elegantly
than by just bombing everything out if we don't get what we want so assuming now that we have
a valid guess we then print that to the console note that again with the print line macro we're
using the curly braces in a string to indicate a spot where we want to insert something supplied
as a parameter now we're going to use match again and this time we're going to use it to
compare our guess against the secret number and this we do with the cmp method the compare method
cmp on an integer returns an object of a type called ordering and an ordering object can be
one of three kinds less greater or equal we can then use that return type in our match statement
to take an appropriate decision about how to handle it this is a little more compact than
using a chain of if statements to compare three times in a row so if we get greater or lesser than
as our results we just print too big or too small but if we have the same number by way of an equal
result with ordering we win and we use break to exit the inner loop and to start another game
so to sum up we've touched on a few key things in rust here the syntax first of all is somewhat
similar to c c plus plus and java and how blocks of code are set aside with braces and
lines are ended with semicolons variables in rust however are immutable by default this is something
important enough that i want to devote an entirely separate video to discussing in detail as this is
a big part of rust's memory model one of its big attractions and macros are also going to be worth
their own video too and finally we use result values to determine the outcomes of certain types
of operations and we can select on those values to take actions of varying degrees of precision
based on what we need so in our next video i'm going to talk in more detail about rust's memory
model i'm going to talk more about immutable and mutable values and about concepts like borrowing
which lies at the heart of how rust ensures memory safety that's going to be a pretty complex dive so
strap it in when we get to it but for now that's it for this video if you liked it please leave a
comment below don't forget to subscribe to the tech talk channel on youtube and for more smart
rust and more dev with serdar generally be sure to follow me on facebook youtube and infowl.com
you