Difference between revisions of "Rust/C2/Variables-and-Mutability-in-Rust/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "Title of the script: Variables and Mutability Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Praveen S Keywords: Rust, cargo, package manager, variab...")
 
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
Author: Jayesh Katta Ramalingaiah
 
Author: Jayesh Katta Ramalingaiah
  
Domain Reviewer:  
+
Domain Reviewer: Vigneshwer Dhinakaran
  
Novice Reviewer: Praveen S
+
Novice Reviewer: Praveen S.
  
 
Keywords: Rust, cargo, package manager, variables, mut
 
Keywords: Rust, cargo, package manager, variables, mut
Line 28: Line 28:
 
* About '''mutability''' and  
 
* About '''mutability''' and  
 
* How to '''mutate variables''' in '''Rust'''
 
* How to '''mutate variables''' in '''Rust'''
 
 
  
 
|-
 
|-
Line 47: Line 45:
 
* You should be familiar with '''compiling''' and '''running Rust''' files'''.'''
 
* You should be familiar with '''compiling''' and '''running Rust''' files'''.'''
 
* If not, please go through the prerequisite '''Rust''' tutorials on this website.
 
* If not, please go through the prerequisite '''Rust''' tutorials on this website.
 
 
  
 
|-
 
|-
 
|| Slide: Code files
 
|| Slide: Code files
 
||  
 
||  
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page.
+
* The file used in this tutorial is available in the''' Code files''' link on this tutorial page.
 
* Pls download and extract the file.
 
* Pls download and extract the file.
 
* Make a copy and then use it for practising.
 
* Make a copy and then use it for practising.
 
 
  
 
|-
 
|-
Line 99: Line 93:
  
 
Type the '''command''' as shown.
 
Type the '''command''' as shown.
 
 
 
  
 
|-
 
|-
Line 122: Line 113:
  
  
Browse and locate the folder “'''variables_mutability'''”.
+
Browse and locate the “'''variables_mutability'''” folder.
  
  
Line 131: Line 122:
  
 
Click on '''variables_mutability'''
 
Click on '''variables_mutability'''
|| Under the '''EXPLORER''' section, expand the '''project''' folder “'''variables_mutability'''” by clicking on it.
+
|| Under '''EXPLORER''' section, expand the '''project “variables_mutability'''” by clicking on it.
  
 
|-
 
|-
 
|| <nowiki>[Editor] </nowiki>Expand '''src '''and click on '''main.rs'''
 
|| <nowiki>[Editor] </nowiki>Expand '''src '''and click on '''main.rs'''
|| Then expand '''src''' and open the '''main.rs''' file.
+
|| Then expand the '''src''' and open the '''main.rs''' file.
  
 
|-
 
|-
Line 158: Line 149:
  
 
Highlight: '''let '''
 
Highlight: '''let '''
|| '''let keyword''' is used to declare '''variables''' in '''Rust.'''
+
|| '''let keyword''' is used to '''declare variables''' in '''Rust.'''
  
 
|-
 
|-
Line 165: Line 156:
  
 
'''let a = 1;'''
 
'''let a = 1;'''
|| Here we have initialized the '''variable a '''and assigned the value '''1''' to it.
+
|| Here we have '''initialized''' the '''variable a '''and assigned the value '''1''' to it.
  
 
|-
 
|-
Line 172: Line 163:
  
 
'''println!("The value of a is {}",a);'''
 
'''println!("The value of a is {}",a);'''
|| Using the '''println '''macro we are printing the value.
+
|| Using the '''println macro''' we are printing the value.
  
 
|-
 
|-
Line 179: Line 170:
  
 
print!("The value of a is '''{}'''",a);
 
print!("The value of a is '''{}'''",a);
|| Here the '''{} '''is the '''format specifier.'''
+
|| Here the '''{} open and close brace'''is a '''format specifier.'''
  
 
|-
 
|-
Line 186: Line 177:
  
 
print!("The value of a is {}",'''a''');
 
print!("The value of a is {}",'''a''');
|| In the '''print method''', the '''variable a''' is after the comma.
+
|| In the '''print method''', the '''variable a''' is after comma.
  
  
Line 227: Line 218:
  
 
'''cargo build '''<nowiki>[Enter]</nowiki>
 
'''cargo build '''<nowiki>[Enter]</nowiki>
|| To '''compile '''the '''Cargo project''', type '''cargo build'''
+
|| To '''compile '''the '''Cargo project''', type '''cargo build'''.
  
 
|-
 
|-
Line 234: Line 225:
  
 
'''cannot assign twice to immutable variable ‘a’'''
 
'''cannot assign twice to immutable variable ‘a’'''
 
 
 
 
|| Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.'''
 
|| Here, we can see an error - '''cannot assign twice to immutable variable ‘a’.'''
  
Line 250: Line 238:
  
 
let''' mut '''a = 1;
 
let''' mut '''a = 1;
|| Now, type '''mut '''in between '''let''' and '''a'''.  
+
|| Now, type '''m u t '''in between '''let''' and '''a'''.  
 
+
 
+
  
  
Line 291: Line 277:
  
 
cargo run  
 
cargo run  
|| After switching back I directly did a '''cargo run''' without '''compiling''' the '''project''' again.
+
|| After switching back, I directly did a '''cargo run''' without '''compiling''' the '''project''' again.
  
  
Line 307: Line 293:
  
  
let’s summarize.
+
Let’s summarize.
  
 
|-
 
|-
 
|| Slide''': '''Summary''' '''
 
|| Slide''': '''Summary''' '''
 
|| In this tutorial, we have learnt:
 
|| In this tutorial, we have learnt:
 
  
 
* How to create '''variables'''
 
* How to create '''variables'''
 
* About '''mutability''' and
 
* About '''mutability''' and
 
* How to '''mutate variables'''
 
* How to '''mutate variables'''
 
 
  
 
|-
 
|-
Line 324: Line 307:
 
|| As an assignment,
 
|| As an assignment,
  
* Go to the project folder '''rust-assignment'''
+
* Go to the '''project''' folder '''rust-assignment'''
 
* In the '''main.rs''' file
 
* In the '''main.rs''' file
 
** Create a '''variable '''named '''s''' and make it '''mutable'''.
 
** Create a '''variable '''named '''s''' and make it '''mutable'''.
Line 331: Line 314:
 
** Reassign the value of '''s''' as '''19'''
 
** Reassign the value of '''s''' as '''19'''
 
** Print the value of '''s'''
 
** Print the value of '''s'''
* '''Compile '''and '''execute '''the project.
+
* '''Compile '''and '''execute '''the '''project'''.
 
* Observe the output in the '''Terminal'''
 
* Observe the output in the '''Terminal'''
 
 
  
 
|-
 
|-
Line 341: Line 322:
 
* The video at the following link summarises the Spoken Tutorial project.
 
* The video at the following link summarises the Spoken Tutorial project.
 
* Please download and watch it
 
* Please download and watch it
 
 
  
 
|-
 
|-
Line 349: Line 328:
 
* We conduct workshops using spoken tutorials and give certificates.
 
* We conduct workshops using spoken tutorials and give certificates.
 
* For more details, please write to us.
 
* For more details, please write to us.
 
 
  
 
|-
 
|-

Latest revision as of 11:18, 21 June 2021

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