Difference between revisions of "Rust/C2/Variables-and-Mutability-in-Rust/English"
(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...") |
Nancyvarkey (Talk | contribs) |
||
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. | ||
− | |||
− | |||
|- | |- | ||
Line 56: | Line 52: | ||
* 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 | + | Browse and locate the “'''variables_mutability'''” folder. |
Line 131: | Line 122: | ||
Click on '''variables_mutability''' | Click on '''variables_mutability''' | ||
− | || Under | + | || 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 | + | || '''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 ''' | + | || 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 | + | || 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 | + | || 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 ''' | + | || 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. | |
|- | |- | ||
|| 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. | ||
− | |||
− | |||
|- | |- |
Revision as of 14:03, 21 January 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 |