[No audio]
This section is about basic programming with Rust.
If you are already familiar with some other
programming language, then this section will give you
a quick start for writing codes in Rust.
You don't need to worry if you are new
as we will explain everything in detail and will
learn everything in a step by step fashion from
complete scratch. In this very first tutorial,
in this section, we will learn two things.
That is comments and basic print commands.
So without any delay, let's get started.
Almost every programming language support
the use of comments.
A comment is a programmer readable, explanation or annotation
in the source code file of a computer program
and they are written with the intention of making
the source code easier to understand.
They are ignored by compilers and are not executed.
Commented code lines in Rust files are
lines that starts with two backslashes.
For instance, this line is going
to be treated as comments.
[No audio]
We can have more comments in the code by adding
more lines that starts with a couple of backslashes.
[No audio]
There are situations in which we may
want to add multiple lines of comments.
In that situation, it is not very convenient
and effective to keep on adding backslashes at
the start of each and every line.
An efficient way of writing multiline comments in Rust
is with the help of backslash and star.
The comment is ended with the
help of star followed by backslash.
Let us add a single multiline comment.
[No audio]
These two types of comments are enough for now
to add necessary documentation and explanation to our code.
We will now cover the second topic of
the lecture which is to learn the basic
print command and some of its variants.
We have already seen the basic print command
in our first Rust program of Hello World.
It is used to print something through the terminal.
Let me add a simple print statement for
printing the text of Hello from Rust.
[No audio]
Please note that ideally you should put semicolons
after the end of each line of code.
Of course, there are some exceptions which will become
evident to us as we progress along the course.
The ln after the print means that after
this command, any subsequent printing to the terminal
will be on the next line.
The exclamation mark is a symbol
to denote a Rust macro.
In simple words, a macro are a
way of writing code that writes other
code which is also known as metaprogramming.
The Rust macros are substituted by other
code before the program is being executed.
Don't worry about them for now as they will make
sense as we progress along the course and cover the
topic of macros in the later on sections.
For now, it is sufficient to know that it is a macro.
To execute the code, we will typically use the cargo
run command by typing cargo run in the terminal.
This command will execute the program and any output
from the program will be displayed on the terminal.
[No audio]
Comments may be inserted within
a Rust statement or command.
It is done with the help of syntax
that we use to write multiline comments.
For instance, consider the Hello World print statement
again. We can command a piece of this statement to
be treated as comment and not part of the source
code by enclosing it inside the multiline comment.
The ln in this case will be treated as comment.
Unlike println command, which moves to the next line
after printing to the terminal, the print does not move
to the next line after printing to the terminal.
Let us now learn how to print strings and numbers.
[No audio]
We cannot print strings or numbers by
just enclosing it inside the print statement.
It will generate an error message.
This is a nice feature of Rust
which is known as compile time errors.
The compile time errors refers to the
error messages that are indicated by the
compiler before we even run the program.
If I move my mouse cursor, it will
give me more details about the error message.
In this case, it says you might
be missing a string literal to format.
If for some reason I do not understand the details
that are being provided by the compiler, so I may
execute this program and it will give me more information.
[No audio]
In this case, it is giving me the
correct syntax indicated by the green text.
Let us explain the problem
in this case. The print function needs
some sort of placeholder inside the double
quotes which will be replaced using the
constants, strings, variables or whatever we mention.
So the correct syntax for printing out the value
of ten will be placeholder inside the double quotes.
Let me add some suitable text
also. During the execution, the placeholder will be
replaced with the value that we have mentioned.
The output in this case will be the
value is ten. If there are multiple placeholders
and multiple values after the double quotes,
so the placeholder will be replaced in the
order in which the parameters are being provided.
The order is always going to be from left to right.
Let us look at printing the strings now.
Strings are indicated with the help of double quotes.
Let us add a print statement for
printing my first name and last name
which are being provided as string arguments.
[No audio]
This statement will print, My first name is
Nouman and my last name is Azam.
The first placeholder was being replaced with the first
parameter after the double quotes and the second placeholder
has been replaced with the second parameter.
To print text on the same line and not on a
new line, we will use the print statement without the ln.
Let me add a print statement.
[No audio]
This text will now be printed on the same line.
Please note that the print will only occur
on the next line when there is no
more spaces to print on the same line.
Let me add some printing statements to see this.
[No audio]
Let us now look at another
print statement. In this case
now we have inserted text inside the print
statement which is spanning more than one line.
We also have a print statement with no
text which will ensure that subsequent printing will
be performed on the next line.
Let us see the output in this case.
[No audio]
You may note that the output
is also spanning multiple lines,
although we have used this simple print statement.
The key point here is that the print or println will
print what it is instructed to print, and
when the printing is complete, the println command will
print subsequent text on next line, while the simple print
will print subsequent printing on the same line.
We will now look at the use of escape sequences.
[No audio]
An escape sequence contains a backslash symbol followed
by one of the escape sequence characters.
It is a combination of characters that has a
meaning other than the literal character contained therein.
We will cover a few of the most important ones.
The first one in this regards is the backslash
n, which is used to ensure that subsequent printing
will be done on the next line.
Adding it two times will mean that one line
will be empty and will contain no text.
Let me add some text also.
[No audio]
Let us confirm the output now.
[No audio]
There are other escape sequences also such as
slash t, which is for inserting a tab.
A tab is essentially spaces of some fixed length.
Let us use it inside a print statement with some text.
[No audio]
Let us see the output now.
[No audio]
You may note some empty spaces at the
beginning of the output on the terminal.
Let us see one more frequently used escape sequence.
The slash r is used for carriage return, which
has its history related to the typewriter which people
used in the old days for typing out documents.
A carriage return moves your carriage all the way
to the right at the start of the line.
So anything that you print afterwards will delete any
previous text that is present on the same line.
Let me add a print statement with some
text and the carriage return escape sequence.
[No audio]
The Rust will first print the text
before the slash r on the screen.
When it encounters slash r, it will move to the beginning
of the same line and will write the remaining text after
the slash r in the print command on the screen.
This will erase the previously
written text on the screen.
Let us see the output of this.
[No audio]
We may note that we are only able to see the text
of, this text will only appear on the screen, and we are
unable to see the first part of the text on the terminal.
A backslash is very useful when
used inside the print statement.
It is used to ignore the effect
of one escape sequence which comes afterwards
and will print it as ordinary text.
For instance, let us add a couple of slash
n inside a print statement with some text.
[No audio]
The first slash n has
an additional backslash before it.
This will ignore its special meaning and therefore the first
slash n will be part of the text, while the
second slash n will have its special meaning.
Let us see the output.
[No audio]
The next use of the backslash is to display
single quotes and double quotes inside the print statement.
Let me add a print statement to explain this.
[No audio]
In this print statement, a single quote will
be displayed in place of the backslash quote,
while a double quote will be displayed in
place of the backslash double quote.
The double quotes in this case will not be
treated as the ending double quotes corresponding to the
starting double quotes of the print statement.
Let us see its output.
The backslash special meaning can be used to
print the backslash itself inside the print statement.
We will use two backslashes in this case.
Let me add a print statement to illustrate this.
[No audio]
In this example now, the first backslash will ignore the
special meaning of the second backslash and
therefore the second backslash will be printed.
One of the main uses of the backslash where
it will be used as part of the printing
text is to display the directory or folder locations.
Next we will look at the positional arguments.
The positional arguments are a list of parameters
or arguments that we pass to the print
command after the double quote separated by commas.
Let me add a print text with some positional arguments.
[No audio]
In this statement, the numbers inside the placeholder
will be replaced by the respective parameters.
The first parameter, which is the string of like,
will be replaced by the placeholder containing one.
The second parameter, which has the value of
20, will be replaced by the placeholder containing
the value of two, and in the same
way the third parameter will also be replaced.
Let us see the respective outputs.
[No audio]
Sometimes it may be more meaningful
to use names instead of numbers.
This means that we will use the names of the
arguments instead of using their numbers inside the placeholders.
Let us look at an example by including a
suitable print statement containing a couple of named arguments.
[No audio]
The first placeholder will contain the value of
the parameter with the name of language and
the second parameter will contain the value of
the parameter with the name of activity.
The order does not matter in this case.
Let us look at one last topic and that
is printing basic Math inside a print statement.
I will add a print statement in this case
which will print the value of some addition.
[No audio]
The placeholder in this example will be
replaced with the value of addition operation.
There are a whole bunch of other details which we
will only cover when they become relevant to us
in the course. We will be covering
basic data types in the next lecture,
so do come back for learning that,
and until then, happy Rust programming.
[No audio]