Difference between revisions of "Rust/C2/Variables-and-Mutability-in-Rust/English"
Nancyvarkey (Talk | contribs) |
Pravin1389 (Talk | contribs) |
||
| Line 49: | Line 49: | ||
|| Slide: Code files | || Slide: Code files | ||
|| | || | ||
| − | * The | + | * 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. | ||
Revision as of 22:23, 1 February 2021
Title of the script: Variables and Mutability
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer: Praveen S
Keywords: Rust, cargo, package manager, variables, mut
| |
|
| Slide: Title | Welcome to the spoken tutorial on “Variables and Mutability in RUST”. |
| Slide:
Learning Objectives |
In this tutorial, we will learn:
|
| Slide: System Specifications | This tutorial is recorded using:
However you may use any other editor of your choice. |
| Slide : Pre-requisites | To practice this tutorial,
|
| Slide: Code files |
|
| Slide: Mutability |
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.
|
| Only Narration | Here onwards, please remember to press the Enter key after typing each command. |
| [Terminal] Type:
[Enter] |
Using cd command go to the Rust practice folder which we created earlier. |
| [Terminal] Type:
|
Let us create a new project named new variables_mutability.
|
| Open Visual Studio Code editor. | You may use any editor of your choice.
|
| [Editor]
Open Folder -> variables_mutability |
Open the created project by clicking on the Open folder link in the Welcome page.
|
| [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:
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:
|
Here we have initialized the variable a and assigned the value 1 to it. |
| [Editor] Highlight:
|
Using the println macro we are printing the value. |
| [Editor] Highlight:
|
Here the {} open and close braceis a format specifier. |
| [Editor] Highlight:
|
In the print method, the variable a is after comma.
|
| [Editor] Highlight:
|
Here, we are trying to reassign the variable to 2. |
| [Editor] Highlight:
|
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:
[Enter] |
Go to the project folder variables_mutability using the cd command. |
| [Terminal] Type:
|
To compile the Cargo project, type cargo build. |
| [Terminal] Highlight:
|
Here, we can see an error - cannot assign twice to immutable variable ‘a’.
|
| 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:
|
Now type cargo run |
| [Terminal] Highlight
|
We can see the output.
|
| [Terminal] Highlight:
|
After switching back, I directly did a cargo run without compiling the project again.
|
| Only narration | With this we have come to the end of this tutorial.
|
| Slide: Summary | In this tutorial, we have learnt:
|
| Slide: Assignment | As an assignment,
|
| Slide: About Spoken Tutorial Project |
|
| Slide: Spoken tutorial workshops |
|
| 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 |