Rust/C2/Variables-and-Mutability-in-Rust/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of the script: Variables and Mutability

Author: Jayesh Katta Ramalingaiah

Domain Reviewer: Vigneshwer Dhinakaran

Novice Reviewer: Praveen S.

Keywords: Rust, cargo, package manager, variables, mut


Visual Cue
Narration
Slide: Title Welcome to the spoken tutorial on “Variables and Mutability in RUST”.
Slide:

Learning Objectives

In this tutorial, we will learn:
  • How to create variables.
  • About mutability and
  • How to mutate variables in Rust
Slide: System Specifications This tutorial is recorded using:
  • Ubuntu Linux OS version 18.04
  • Rust version 1.47.0
  • Visual Studio Code version 1.45.0 (code editor)

However you may use any other editor of your choice.

Slide : Pre-requisites To practice this tutorial,
  • You should be familiar with compiling and running Rust files.
  • If not, please go through the prerequisite Rust tutorials on this website.
Slide: Code files
  • The file used in this tutorial is available in the Code files link on this tutorial page.
  • Pls download and extract the file.
  • Make a copy and then use it for practising.
Slide: Mutability
  • In Rust, variables are immutable by default.
  • That means, once the variable is declared we cannot change its value.
  • This is one of many advantages in Rust
  • That encourages us to write our code in a way that,
  • It takes advantage of the safety and concurrency that Rust offers.

To understand this let us create a project and see how this works.

Press Ctrl+Alt+T keys Open the terminal by pressing Ctrl,Alt and T keys simultaneously on the keyboard.


Ensure that you have root permissions to run the commands.

Only Narration Here onwards, please remember to press the Enter key after typing each command.
[Terminal] Type:


cd Desktop/MyRustProject

[Enter]

Using cd command go to the Rust practice folder which we created earlier.
[Terminal] Type:


cargo new variables_mutability [Enter]

Let us create a new project named new variables_mutability.


Type the command as shown.

Open Visual Studio Code editor. You may use any editor of your choice.


I will use Visual Studio Code editor for this demonstration.

[Editor]


Welcome Page ->

Open Folder ->

variables_mutability

Open the created project by clicking on the Open folder link in the Welcome page.


Browse and locate the “variables_mutability” folder.


Then click on the OK button at the top right corner.

[Editor]

Click on variables_mutability

Under EXPLORER section, expand the project “variables_mutability” by clicking on it.
[Editor] Expand src and click on main.rs Then expand the src and open the main.rs file.
[Editor] Type:


fn main(){

let a = 1;

print!(“The value of a is {}”,a);

a=2;

print!(“The value of a is {}”,a);

}

In the editor, replace the code as shown.
[Editor]

Highlight: let

let keyword is used to declare variables in Rust.
[Editor] Highlight:


let a = 1;

Here we have initialized the variable a and assigned the value 1 to it.
[Editor] Highlight:


println!("The value of a is {}",a);

Using the println macro we are printing the value.
[Editor] Highlight:


print!("The value of a is {}",a);

Here the {} open and close braceis a format specifier.
[Editor] Highlight:


print!("The value of a is {}",a);

In the print method, the variable a is after comma.


It is the value for the respective format specifier.

[Editor] Highlight:


a = 2;

Here, we are trying to reassign the variable to 2.
[Editor] Highlight:


print!("The value of a is {}",a);

After reassigning we are printing the variable’s value to see what does the value contain.
Ctrl + S Save the file.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cd variables_mutability

[Enter]

Go to the project folder variables_mutability using the cd command.
[Terminal] Type:


cargo build [Enter]

To compile the Cargo project, type cargo build.
[Terminal] Highlight:


cannot assign twice to immutable variable ‘a’

Here, we can see an error - cannot assign twice to immutable variable ‘a’.


Recall in the beginning of the tutorial I had mentioned that variables are immutable by default.

Switch to Editor So switch back to the editor.
[Editor] Type:

let mut a = 1;

Now, type m u t in between let and a.


[Editor] Highlight:

let mut a = 1;

Here we are telling the compiler that the initialized variable is mutable.
Ctrl + S Save the file.
Switch to terminal Switch back to the terminal.
[Terminal] Type:


cargo run [Enter]

Now type cargo run
[Terminal] Highlight


The value of a is 1The value of a is 2

We can see the output.


Both the initialized and the modified values are printed successfully.

[Terminal] Highlight:


cargo run

After switching back, I directly did a cargo run without compiling the project again.


Did you notice that?


When we do a cargo run the package manager will build again and run the project.


So going forward, we can directly use cargo run to build and run the project in one go.

Only narration With this we have come to the end of this tutorial.


Let’s summarize.

Slide: Summary In this tutorial, we have learnt:
  • How to create variables
  • About mutability and
  • How to mutate variables
Slide: Assignment As an assignment,
  • Go to the project folder rust-assignment
  • In the main.rs file
    • Create a variable named s and make it mutable.
    • Assign 13 as a value to s
    • Print the value of s
    • Reassign the value of s as 19
    • Print the value of s
  • Compile and execute the project.
  • Observe the output in the Terminal
Slide: About Spoken Tutorial Project
  • The video at the following link summarises the Spoken Tutorial project.
  • Please download and watch it
Slide: Spoken tutorial workshops
  • We conduct workshops using spoken tutorials and give certificates.
  • For more details, please write to us.
Slide: Forum questions Pls post your timed queries in this forum
Slide: Acknowledgement Spoken Tutorial Project is funded by Ministry of Education, Government of India
Slide: Thanks This is Jayesh signing off. Thank you for joining

Contributors and Content Editors

Kr.jayesh, Nancyvarkey, Pravin1389