Difference between revisions of "Rust-Programming-Language/C2/Borrowing/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
 +
 
{| border="1"
 
{| border="1"
 
|-
 
|-
Line 5: Line 6:
 
|-
 
|-
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Borrowing in Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Borrowing in Rust.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 2'''</div>
+
|| ''Slide 2'''
  
<div style="color:#000000;">'''Learning Objectives'''</div>
+
''Learning Objectives'''
 
|| In this tutorial, we will learn about:
 
|| In this tutorial, we will learn about:
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Borrowing and</div>
+
* Borrowing and
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Rules of borrowing</div>
+
* Rules of borrowing
  
|- style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-
|| <div style="color:#000000;">'''Slide 3'''</div>
+
|| ''Slide 3'''
  
<div style="color:#000000;">'''System Requirements'''</div>
+
''System Requirements'''
| style="color:#000000;" | To record this tutorial I’m using the following setup
+
|| To record this tutorial I’m using the follwoing setup
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''5'''</span>
+
|| '''Slide 4'''  
  
<div style="color:#000000;">'''Code Files'''</div>
+
''Code Files'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">The following code file is required to practise this tutorial.</div>
+
* The following code file is required to practise this tutorial.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">This file is provided in the </span><span style="color:#000000;">'''Code Files'''</span><span style="color:#000000;"> link of this tutorial page.</span></div>
+
* This file is provided in the '''Code Files''' link of this tutorial page.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 6:'''</div>
+
|| ''Slide 5'''
  
<div style="color:#000000;">'''Borrowing'''</div>
+
''Borrowing'''
 
|| Let us learn about borrowing.
 
|| Let us learn about borrowing.
  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Borrowing is the act of creating references to data without taking ownership</div>
+
* Borrowing is the act of creating references to data without taking ownership
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">When we borrow a value, we reference its memory address with the & </div>
+
* When we borrow a value, we reference its memory address with the &  
  
<div style="color:#000000;"></div>
 
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
 
|| <div style="color:#000000;">'''Slide 7 : '''</div>
 
  
<div style="color:#000000;">'''Borrowing - Example'''</div>
+
|-
 +
|| ''Slide 6 '''
  
<div style="color:#000000;">fn main() {</div>
+
''Borrowing - Example'''
  
<div style="color:#000000;">let s = String::from("hello");</div>
+
fn main() {
  
<div style="color:#000000;">let s1 = &s;</div>
+
let s = String::from("hello");
  
<div style="color:#000000;">}</div>
+
let s1 = &s;
 +
 
 +
}
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">Here <span style="color:#000000;"></span><span style="color:#000000;">'''s’'''</span><span style="color:#000000;"> owns the string “</span><span style="color:#000000;">'''hello'''</span><span style="color:#000000;"></span></div>
+
* Here ‘'''s’''' owns the string “'''hello'''”
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;"></span><span style="color:#000000;">'''s1'''</span><span style="color:#000000;">’ borrows ‘</span><span style="color:#000000;">'''s'''</span><span style="color:#000000;">’ by reference denoted by </span><span style="color:#000000;">'''&s'''</span><span style="color:#000000;">. </span></div>
+
* ‘'''s1'''’ borrows ‘'''s'''’ by reference denoted by '''&s'''.  
<span style="color:#000000;">But </span>ownership<span style="color:#000000;"> of the string is still with </span><span style="color:#000000;">'''s.'''</span>
+
But ownership of the string is still with '''s.'''
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''s1 '''</span><span style="color:#000000;">is only allowed to read the data</span></div>
+
* '''s1 '''is only allowed to read the data
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">It will point to the data, making it an immutable reference to </span><span style="color:#000000;">'''s.'''</span></div>
+
* It will point to the data, making it an immutable reference to '''s.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <span style="color:#252525;">Next we will see about rules of borrowing</span><span style="color:#252525;">.</span>
+
|| Next we will see about rules of borrowing.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| '''Slide 8: <span style="color:#000000;">Rules of borrowing:'''</span>
+
|| '''Slide 7'''
  
<div style="color:#000000;">1. Any number of Immutable references to a data</div>
+
''' Rules of borrowing: Rule 1'''
 +
 
 +
1. Any number of Immutable references to a data
  
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">You can have any number of immutable references to a data at the same time.</div>
+
* You can have any number of immutable references to a data at the same time.
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Immutable references do not allow modification of the data.</div>
+
* Immutable references do not allow modification of the data.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Any number of read operations are </span>safe,<span style="color:#000000;"> </span>but <span style="color:#000000;">write </span>operations are not<span style="color:#000000;"> allowed.</span></div>
+
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-
| style="color:#000000;" |  
+
|| '''Slide 8'''
| style="color:#000000;" | Let us understand with an example.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <span style="color:#000000;">Open </span><span style="color:#000000;">'''Visual'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''code editor'''</span>
+
|| <span style="color:#000000;">Open the </span><span style="color:#000000;">'''Visual </span><span style="color:#000000;">S</span><span style="color:#000000;">tudio code editor.'''</span>
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''Terminal'''</span><span style="color:#000000;"> and select</span><span style="color:#000000;">''' New Terminal'''</span><span style="color:#000000;">.</span>
+
  
|| <span style="color:#000000;">In the menu bar, click on </span><span style="color:#000000;">'''Terminal'''</span><span style="color:#000000;"> and select</span><span style="color:#000000;">''' New Terminal'''</span><span style="color:#000000;">.</span>
+
''' Rules of borrowing: Rule 1'''
 +
||
 +
* Any number of read operations are safe, but write operations are not allowed.
 +
|-
 +
||
 +
|| Let us understand with an example.
 +
|-
 +
|| Open '''Visual''' '''code editor'''
 +
|| Open the '''Visual Studio code editor.'''
 +
|-
 +
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| In the menu bar, click on '''Terminal''' and select''' New Terminal'''.
|| <span style="color:#000000;">'''> cd'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''Desktop'''</span><span style="color:#000000;">/</span><span style="color:#000000;">'''MyRustProject'''</span>
+
  
<span style="color:#000000;">'''>'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''cargo new borrow'''</span>
+
|-
 +
|| '''> cd''' '''Desktop'''/'''MyRustProject'''
  
<div style="color:#000000;">'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> borrow'''</div>
+
'''>''' '''cargo new borrow'''
|| <span style="color:#000000;">Let us go to our working directory </span><span style="color:#000000;">'''MyRustProject'''</span><span style="color:#000000;"> as explained earlier.</span>
+
  
<span style="color:#000000;">Type the command </span><span style="color:#000000;">'''cargo new borrow '''</span><span style="color:#000000;">and press </span><span style="color:#000000;">'''Enter.'''</span>
+
''In the menu bar, File >> Open folder
  
<div style="color:#000000;">Open the created project as shown.</div>
+
>> Desktop >> MyRustProject >> borrow'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| Let us go to our working directory '''MyRustProject''' as explained earlier.
|| <span style="color:#000000;">Point to the </span><span style="color:#000000;">'''main.rs'''</span><span style="color:#000000;"> file.</span>
+
  
|| <span style="background-color:#ffffff;color:#000000;">In the </span><span style="background-color:#ffffff;color:#000000;">'''main.rs '''</span><span style="background-color:#ffffff;color:#000000;">file, copy and paste the code from the </span><span style="background-color:#ffffff;color:#000000;">'''Code file'''</span><span style="background-color:#ffffff;color:#000000;">.</span>
+
Type the command '''cargo new borrow '''and press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">fn main() {</div>
+
  
<div style="color:#000000;">let a = String::from("Hello world");</div>
+
Open the created project as shown.
 +
|-
 +
|| Point to the '''main.rs''' file.
  
<div style="color:#000000;">let b = &a;</div>
+
|| In the '''main.rs '''file, copy and paste the code from the '''Code file'''.
 +
|-
 +
|| fn main() {
  
<div style="color:#000000;">let c = &a;</div>
+
let a = String::from("Hello world");
  
<div style="color:#000000;">let d = &a;</div>
+
let b = &a;
  
<div style="color:#000000;">println!("{}", b);</div>
+
let c = &a;
  
<div style="color:#000000;">println!("{}", c);</div>
+
let d = &a;
  
<div style="color:#000000;">println!("{}", d);</div>
+
println!("{}", b);
  
<div style="color:#000000;">}</div>
+
println!("{}", c);
|| <span style="color:#000000;">Variables </span><span style="color:#000000;">'''b, c, d '''</span><span style="color:#000000;">are assigned with an immutable reference to </span><span style="color:#000000;">'''a'''</span><span style="color:#000000;">.</span>
+
  
<div style="color:#000000;">When you try to print them, there won't be any errors.</div>
+
println!("{}", d);
  
<span style="color:#000000;">It </span>allows multiple<span style="color:#000000;"> immutable references to a piece of data simultaneously.</span>
+
}
 +
|| Variables '''b, c, d '''are assigned with an immutable reference to '''a'''.
  
<div style="color:#000000;">Save the program.</div>
+
When you try to print them, there won't be any errors.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
It allows multiple immutable references to a piece of data simultaneously.
||  
+
 
 +
Save the program.
 +
 
 +
|-
 +
|| Type cargo run.
 
|| Run the code to see the output.
 
|| Run the code to see the output.
  
O<span style="color:#000000;">utput shows, variable </span>'''a'''<span style="color:#000000;"> can have </span>multiple <span style="color:#000000;">immutable references.</span>
+
Output shows, variable '''a''' can have multiple immutable references.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slid</span>e 9<span style="color:#000000;">: Rules of borrowing:'''</span>
+
|| '''Slide 9'''
  
<div style="color:#000000;">2. Only 1 mutable reference to a data</div>
+
''' Rules of borrowing: Rule 2'''
| style="color:#000000;" | The second rule states that you can have only one mutable reference to a data.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#252525;" |
+
|| <div style="color:#000000;">Let us see an example. </div>
+
  
<span style="color:#000000;">Clear the previous code. Copy paste the code from the </span>C<span style="color:#000000;">ode file.</span>
+
2. Only 1 mutable reference to a data
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| The second rule states that you can have only one mutable reference to a data.
|| <div style="color:#000000;">fn main() {</div>
+
|-  
 +
||  
 +
|| Let us see an example.
  
<div style="color:#000000;">let mut s = String::from("Hello ");</div>
+
Clear the previous code. Copy paste the code from the Code file.
 +
|-
 +
|| fn main() {
  
<div style="color:#000000;">let r1 = &mut s;</div>
+
let mut s = String::from("Hello ");
  
<div style="color:#000000;">let r2 = &mut s;</div>
+
let r1 = &mut s;
  
<div style="color:#000000;">r1.push_str("World"); r2.push_str("Rust");</div>
+
let r2 = &mut s;
  
<div style="color:#000000;">println!("{}",r1);</div>
+
r1.push_str("World"); r2.push_str("Rust");
  
<div style="color:#000000;">println!("{}",r2);</div>
+
println!("{}",r1);
  
<div style="color:#000000;">}</div>
+
println!("{}",r2);
|| <span style="color:#000000;">Here, the mutable variable </span><span style="color:#000000;">'''s'''</span><span style="color:#000000;"> is assigned to a string object </span><span style="color:#000000;">'''hello.'''</span>
+
  
<div style="color:#000000;">Variables r1, r2 are assigned with a mutable reference to s.</div>
+
}
 +
|| Here, the mutable variable '''s''' is assigned to a string object '''hello.'''
  
<div style="color:#000000;">When both the references try to modify the data, rust wont allow this.</div>
+
Variables r1, r2 are assigned with a mutable reference to s.
  
<div style="color:#000000;">It will lead to a data race, where the data gets corrupted.</div>
+
When both the references try to modify the data, rust wont allow this.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">fn main() {</div>
+
  
<div style="color:#000000;">let mut s = String::from("Hello ");</div>
+
It will lead to a data race, where the data gets corrupted.
 +
|-
 +
|| fn main() {
  
<div style="color:#000000;">let r1 = &mut s;</div>
+
let mut s = String::from("Hello ");
  
<span style="color:#000000;">'''//let r2 = &mut s;'''</span>
+
let r1 = &mut s;
  
<div style="color:#000000;">r1.push_str("world"); </div>
+
'''//let r2 = &mut s;'''
  
<span style="color:#000000;">'''//r2.push_str("Rust");'''</span>
+
r1.push_str("world");  
  
<div style="color:#000000;">println!("{}",r1);</div>
+
'''//r2.push_str("Rust");'''
  
<div style="color:#000000;">'''//println!("{}",r2);'''</div>
+
println!("{}",r1);
  
<div style="color:#000000;">}</div>
+
''//println!("{}",r2);'''
|| <div style="color:#000000;">Run the code and check the error.</div>
+
  
<span style="color:#000000;">Let us </span>comment the code, related to the second<span style="color:#000000;"> mutable reference.</span>
+
}
 +
|| Run the code and check the error.
  
<div style="color:#000000;">Save the program.</div>
+
Let us comment the code, related to the second mutable reference.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
| style="color:#000000;" |  
+
Save the program.
 +
|-  
 +
|| Type cargo run.
 
|| Run the code to see the output.
 
|| Run the code to see the output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 10:'''</div>
+
|| ''Slide 10'''
  
<div style="color:#000000;">'''Rules of Borrowing: The third Rule:'''</div>
+
''Rules of Borrowing: Rule 3'''
  
|| <span style="color:#000000;">You </span><span style="color:#000000;">cannot mix</span><span style="color:#000000;">''' mutable '''</span><span style="color:#000000;">and</span><span style="color:#000000;">''' immutable references '''</span><span style="color:#000000;">to the same data at the same time.</span>
+
|| You cannot mix''' mutable '''and''' immutable references '''to the same data at the same time.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
|| <div style="color:#000000;">Let us understand through an example.</div>
+
|| Let us understand through an example.
  
<span style="color:#000000;">Copy and paste the code from the </span>'''C<span style="color:#000000;">ode file'''</span><span style="color:#000000;">.</span>
+
Copy and paste the code from the '''Code file'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">fn main() {</div>
+
|| fn main() {
  
<div style="color:#000000;">let mut s = String::from("Hello ");</div>
+
let mut s = String::from("Hello ");
  
<div style="color:#000000;">let r1 = &mut s;</div>
+
let r1 = &mut s;
  
<span style="color:#000000;">'''//</span> <span style="color:#000000;">let r2 = &s;'''</span>
+
'''// let r2 = &s;'''
  
<div style="color:#000000;">r1.push_str("Rust");</div>
+
r1.push_str("Rust");
  
<div style="color:#000000;">println!("{}", r1);</div>
+
println!("{}", r1);
  
<div style="color:#000000;">}</div>
+
}
|| <span style="background-color:#ffffff;color:#000000;">Mutable variable</span><span style="background-color:#ffffff;color:#000000;">''' s '''</span><span style="background-color:#ffffff;color:#000000;">is assigned to a string object </span><span style="background-color:#ffffff;color:#000000;">'''hello'''</span><span style="background-color:#ffffff;color:#000000;">.</span>
+
|| Mutable variable''' s '''is assigned to a string object '''hello'''.
  
<span style="background-color:#ffffff;color:#000000;">Here </span><span style="background-color:#ffffff;color:#000000;">'''r1 is '''</span><span style="background-color:#ffffff;color:#000000;">assigned </span><span style="background-color:#ffffff;">as </span><span style="background-color:#ffffff;color:#000000;">a mutable reference to s. </span>
+
Here '''r1 is '''assigned ;">as a mutable reference to s.  
  
<span style="background-color:#ffffff;">And</span><span style="background-color:#ffffff;">''' </span><span style="background-color:#ffffff;color:#000000;">r2'''</span><span style="background-color:#ffffff;color:#000000;"> </span><span style="background-color:#ffffff;">as</span><span style="background-color:#ffffff;color:#000000;"> </span><span style="background-color:#ffffff;">an immutable</span><span style="background-color:#ffffff;color:#000000;"> reference to </span><span style="background-color:#ffffff;color:#000000;">'''s'''</span><span style="background-color:#ffffff;color:#000000;">.&nbsp;</span>
+
;">And;">''' r2''' ;">as ;">an immutable reference to '''s'''.&nbsp;
  
 
One reference tries to modify the data while another reference tries to read data.
 
One reference tries to modify the data while another reference tries to read data.
  
<div style="color:#000000;">It will lead to corrupt data because the reference didn’t get out of scope.</div>
+
It will lead to corrupt data because the reference didn’t get out of scope.
  
<div style="color:#000000;">It still has the mutable reference and may change the data in the scope.</div>
+
It still has the mutable reference and may change the data in the scope.
  
<span style="color:#000000;">Comment the </span>line of <span style="background-color:#ffffff;color:#000000;">immutable reference to </span><span style="background-color:#ffffff;color:#000000;">'''s</span><span style="background-color:#ffffff;"> '''</span><span style="background-color:#ffffff;">as shown.</span>
+
Comment the line of immutable reference to '''s;"> ''';">as shown.
 +
 
 +
Save the program.
 +
|-
 +
|| In the terminal, type '''cargo run '''.
 +
|| In the terminal, type '''cargo run '''and see the output.
 +
 
 +
Hence rust doesn't allow you to break all the above 3 rules.
 +
|-
 +
||
 +
|| Next we will see how dangling references occur and how Rust prevents them.
 +
|-
 +
||
 +
||  Let us understand with an example.
  
<div style="color:#000000;">Save the program.</div>
+
Copy and paste the code from the code file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
|| fn get_reference() -> &String {
|| <span style="color:#000000;">In the terminal, type </span><span style="color:#000000;">'''cargo run '''</span><span style="color:#000000;">and see the output.</span>
+
  
<div style="color:#000000;">Hence rust doesn't allow you to break all the above 3 rules.</div>
+
let s = String::from("Hello, Rust!");
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
| style="color:#000000;" | Next we will see how dangling references occur and how Rust prevents them.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
|| <div style="color:#171717;">Let us understand with an example.</div>
+
  
<div style="color:#171717;">Copy and paste the code from the code file.</div>
+
&s // ❌ Error: `s` is dropped at the end of this function
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">fn get_reference() -> &String {</div>
+
  
<div style="color:#000000;">let s = String::from("Hello, Rust!");</div>
+
} // `s` goes out of scope here, but we're trying to return a reference to it
  
<div style="color:#000000;">&s // ❌ Error: `s` is dropped at the end of this function</div>
+
fn main() {
  
<div style="color:#000000;">} // `s` goes out of scope here, but we're trying to return a reference to it</div>
+
let r = get_reference(); // Trying to use a reference to `s`, but `s` is already dropped
  
<div style="color:#000000;">fn main() {</div>
+
println!("{}", r);
  
<div style="color:#000000;">let r = get_reference(); // Trying to use a reference to `s`, but `s` is already dropped</div>
+
}
 +
|| Inside '''get_reference(),''' we create a String named '''s'''.
  
<div style="color:#000000;">println!("{}", r);</div>
+
In the next line, it returns a reference to that string.
  
<div style="color:#000000;">}</div>
+
When the function ends, s goes out of scope, and its heap memory is freed.
|| <span style="color:#000000;">Inside </span><span style="color:#000000;">'''get_reference(),'''</span><span style="color:#000000;"> we create a String named </span><span style="color:#000000;">'''s'''</span><span style="color:#000000;">.</span>
+
  
<span style="color:#000000;">In the next line, it returns </span>a<span style="color:#000000;"> reference to that string.</span>
+
Here we are trying to return a reference to this freed memory.
  
<span style="color:#000000;">When the function ends, s goes </span><span style="color:#000000;">out of scope, </span><span style="color:#000000;">and its heap memory is freed.</span>
+
Hence, we will get an error.
  
Here <span style="color:#000000;">we are trying to return a reference to t</span>his<span style="color:#000000;"> freed memory.</span>
+
When we try to run the main function, it leads to an invalid memory.( a '''dangling reference''').
  
<div style="color:#000000;">Hence, we will get an error.</div>
+
Save the program.
 +
|-
 +
|| In the terminal, type '''cargo run '''.
 +
|| Let us run the program and check the error.
 +
|-
 +
|| ''fn get_reference() -> String {'''
  
<span style="color:#000000;">When we try to run the main function, it </span>l<span style="color:#000000;">eads to an invalid memory.</span>(<span style="color:#000000;"> a </span><span style="color:#000000;">'''dangling reference'''</span><span style="color:#000000;">).</span>
+
''let s = String::from("Hello, Rust!");'''
  
<span style="color:#000000;">Save the program</span>.
+
''s // ownership is moved out safely'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
| style="color:#000000;" | Let us run the program and check the error.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <div style="color:#000000;">'''fn get_reference() -> String {'''</div>
+
  
<div style="color:#000000;">'''let s = String::from("Hello, Rust!");'''</div>
+
''} '''
 +
|| Let's see how to correct this program?
  
<div style="color:#000000;">'''s // ownership is moved out safely'''</div>
+
Let us modify the program to return a value not the reference.
  
<div style="color:#000000;">'''} '''</div>
+
Remove the''' &''' from '''s.'''
|| Let's see h<span style="color:#000000;">ow to correct this program?</span>
+
  
<span style="color:#000000;">Let us modify the program to return </span>a<span style="color:#000000;"> value not the reference.</span>
+
Save the program.
 +
|-
 +
|| In the terminal, type '''cargo run '''.
 +
|| Now run the program and see the result.
 +
|-
 +
|| '''Slide  11'''
  
<span style="color:#000000;">Remove the</span><span style="color:#000000;">''' &'''</span><span style="color:#000000;"> from </span><span style="color:#000000;">'''s.'''</span>
+
* Borrowing
 +
* Rules of borrowing
  
<div style="color:#000000;">Save the program.</div>
+
|| This brings us to the end of this tutorial.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
| style="color:#000000;" | Now run the program and see the result.
+
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
| style="color:#000000;" |
+
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
  
<div style="color:#000000;">Let us summarize. </div>
+
Let us summarize.  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 8'''</div>
+
|| ''Slide 12''
  
<div style="color:#000000;">'''Assignment'''</div>
+
''Assignment'''
  
<div style="color:#000000;">'''fn main() {'''</div>
+
''fn main() {'''
  
<div style="color:#000000;">'''let fruit1 = String::from("Apple");'''</div>
+
''let fruit1 = String::from("Apple");'''
  
<div style="color:#000000;">'''let fruit2 = fruit1; '''</div>
+
''let fruit2 = fruit1; '''
  
<div style="color:#000000;">'''println!("fruit1 = {}", fruit1); '''</div>
+
''println!("fruit1 = {}", fruit1); '''
  
<div style="color:#000000;">'''println!("fruit2 = {}", fruit2);'''</div>
+
''println!("fruit2 = {}", fruit2);'''
  
<div style="color:#000000;">'''}'''</div>
+
''}'''
 
|| As an assignment,
 
|| As an assignment,
  
 
Run the above code and fix the error.
 
Run the above code and fix the error.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 9 '''</div>
+
|| ''Slide 9 '''
  
<div style="color:#000000;">'''Thank You'''</div>
+
''Thank You'''
 
|| Thank you for joining.
 
|| Thank you for joining.
 
|-
 
|-
 
|}
 
|}
<div style="margin-left:-1.588cm;margin-right:-1.429cm;"></div>
 

Revision as of 16:52, 7 October 2025

Visual Cue Narration
Slide 1 Welcome to the Spoken Tutorial on Borrowing in Rust.
Slide 2'

Learning Objectives'

In this tutorial, we will learn about:
  • Borrowing and
  • Rules of borrowing
Slide 3'

System Requirements'

To record this tutorial I’m using the follwoing setup
Slide 4

Code Files'

  • The following code file is required to practise this tutorial.
  • This file is provided in the Code Files link of this tutorial page.
Slide 5'

Borrowing'

Let us learn about borrowing.
  • Borrowing is the act of creating references to data without taking ownership
  • When we borrow a value, we reference its memory address with the &


Slide 6 '

Borrowing - Example'

fn main() {

let s = String::from("hello");

let s1 = &s;

}

  • Here ‘s’ owns the string “hello
  • s1’ borrows ‘s’ by reference denoted by &s.

But ownership of the string is still with s.

  • s1 is only allowed to read the data
  • It will point to the data, making it an immutable reference to s.
Next we will see about rules of borrowing.
Slide 7

Rules of borrowing: Rule 1

1. Any number of Immutable references to a data

  • You can have any number of immutable references to a data at the same time.
  • Immutable references do not allow modification of the data.
Slide 8

Rules of borrowing: Rule 1

  • Any number of read operations are safe, but write operations are not allowed.
Let us understand with an example.
Open Visual code editor Open the Visual Studio code editor.
In the menu bar, click on Terminal and select New Terminal. In the menu bar, click on Terminal and select New Terminal.
> cd Desktop/MyRustProject

> cargo new borrow

In the menu bar, File >> Open folder

>> Desktop >> MyRustProject >> borrow

Let us go to our working directory MyRustProject as explained earlier.

Type the command cargo new borrow and press Enter.

Open the created project as shown.

Point to the main.rs file. In the main.rs file, copy and paste the code from the Code file.
fn main() {

let a = String::from("Hello world");

let b = &a;

let c = &a;

let d = &a;

println!("{}", b);

println!("{}", c);

println!("{}", d);

}

Variables b, c, d are assigned with an immutable reference to a.

When you try to print them, there won't be any errors.

It allows multiple immutable references to a piece of data simultaneously.

Save the program.

Type cargo run. Run the code to see the output.

Output shows, variable a can have multiple immutable references.

Slide 9

Rules of borrowing: Rule 2

2. Only 1 mutable reference to a data

The second rule states that you can have only one mutable reference to a data.
Let us see an example.

Clear the previous code. Copy paste the code from the Code file.

fn main() {

let mut s = String::from("Hello ");

let r1 = &mut s;

let r2 = &mut s;

r1.push_str("World"); r2.push_str("Rust");

println!("{}",r1);

println!("{}",r2);

}

Here, the mutable variable s is assigned to a string object hello.

Variables r1, r2 are assigned with a mutable reference to s.

When both the references try to modify the data, rust wont allow this.

It will lead to a data race, where the data gets corrupted.

fn main() {

let mut s = String::from("Hello ");

let r1 = &mut s;

//let r2 = &mut s;

r1.push_str("world");

//r2.push_str("Rust");

println!("{}",r1);

//println!("{}",r2);'

}

Run the code and check the error.

Let us comment the code, related to the second mutable reference.

Save the program.

Type cargo run. Run the code to see the output.
Slide 10'

Rules of Borrowing: Rule 3'

You cannot mix mutable and immutable references to the same data at the same time.
Let us understand through an example.

Copy and paste the code from the Code file.

fn main() {

let mut s = String::from("Hello ");

let r1 = &mut s;

// let r2 = &s;

r1.push_str("Rust");

println!("{}", r1);

}

Mutable variable s is assigned to a string object hello.

Here r1 is assigned ;">as a mutable reference to s.

">And;"> r2 ;">as ;">an immutable reference to s

One reference tries to modify the data while another reference tries to read data.

It will lead to corrupt data because the reference didn’t get out of scope.

It still has the mutable reference and may change the data in the scope.

Comment the line of immutable reference to s;"> ;">as shown.

Save the program.

In the terminal, type cargo run . In the terminal, type cargo run and see the output.

Hence rust doesn't allow you to break all the above 3 rules.

Next we will see how dangling references occur and how Rust prevents them.
Let us understand with an example.
Copy and paste the code from the code file.
fn get_reference() -> &String {

let s = String::from("Hello, Rust!");

&s // ❌ Error: `s` is dropped at the end of this function

} // `s` goes out of scope here, but we're trying to return a reference to it

fn main() {

let r = get_reference(); // Trying to use a reference to `s`, but `s` is already dropped

println!("{}", r);

}

Inside get_reference(), we create a String named s.

In the next line, it returns a reference to that string.

When the function ends, s goes out of scope, and its heap memory is freed.

Here we are trying to return a reference to this freed memory.

Hence, we will get an error.

When we try to run the main function, it leads to an invalid memory.( a dangling reference).

Save the program.

In the terminal, type cargo run . Let us run the program and check the error.
fn get_reference() -> String {'

let s = String::from("Hello, Rust!");'

s // ownership is moved out safely'

} '

Let's see how to correct this program?

Let us modify the program to return a value not the reference.

Remove the & from s.

Save the program.

In the terminal, type cargo run . Now run the program and see the result.
Slide 11
  • Borrowing
  • Rules of borrowing
This brings us to the end of this tutorial.

Let us summarize.

Slide 12

Assignment'

fn main() {'

let fruit1 = String::from("Apple");'

let fruit2 = fruit1; '

println!("fruit1 = {}", fruit1); '

println!("fruit2 = {}", fruit2);'

}'

As an assignment,

Run the above code and fix the error.

Slide 9 '

Thank You'

Thank you for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat