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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || '''Slide 1''' || <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000...")
 
 
Line 5: Line 5:
 
|-
 
|-
 
|| '''Slide 1'''
 
|| '''Slide 1'''
|| <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#000000;">'''Ownership</span> <span style="color:#000000;">in</span> <span style="color:#000000;">Rust.'''</span>
+
|| Welcome to the Spoken Tutorial on '''Ownership''' 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;">Ownership</div>
+
* Ownership
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Ownership Rules</div>
+
* Ownership Rules
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Drop and Copy </span>traits</div>
+
* Drop and Copy traits
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 3'''</div>
+
|| '''Slide 3'''
  
<div style="color:#000000;">'''System Requirements'''</div>
+
'''System Requirements'''
|| <span style="color:#000000;">To record this tutorial </span>I am<span style="color:#000000;"> using:</span>
+
|| To record this tutorial I am using:
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Ubuntu Linux OS version 22.04</div>
+
* Ubuntu Linux OS version 22.04
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Rust version 1.80.0 </div>
+
* Rust version 1.80.0  
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">Visual Studio Code v</span>ersion<span style="color:#000000;"> 1.19.0</span></div>
+
* Visual Studio Code version 1.19.0
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.101cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">'''Slide 4'''</div>
+
|| '''Slide 4'''
  
<div style="color:#000000;">'''Prerequisites'''</div>
+
'''Prerequisites'''
|| <span style="color:#000000;">To follow this tutorial, you should be familiar with compiling and running a </span><span style="color:#000000;">'''Rust'''</span><span style="color:#000000;"> program.</span>
+
|| To follow this tutorial, you should be familiar with compiling and running a '''Rust''' program.
|- 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 5'''
  
<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;">Ownership.rs</div>
+
* Ownership.rs
* <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 6'''
  
<div style="color:#000000;">'''Ownership'''</div>
+
'''Ownership'''
|| Let us now learn about''' Ownership.'''
+
|| Let us now learn about ''' Ownership'''.
  
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">'''Ownership'''</span><span style="color:#000000;"> allows memory safety without a garbage collector.</span></div>
+
* '''Ownership''' allows memory safety without a garbage collector.
* <div style="color:#171717;margin-left:1.27cm;margin-right:0cm;">It is a key feature of the Rust language that allows it to be both safe and fast.</div>
+
* It is a key feature of Rust language that allows it to be both safe and fast.
  
|- 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> 7'''
+
|| '''Slide 7'''
  
<div style="color:#000000;">'''Ownership Rules'''</div>
+
'''Ownership Rules'''
 
||  
 
||  
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">Each value in Rust has a single owner</div>
+
* Each value in Rust has a single owner
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">There can be only one owner at a time</div>
+
* There can be only one owner at a time
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">When the owner goes out of scope the value will be dropped.</div>
+
* When the owner goes out of scope the value will be dropped.
  
|- 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> 8'''
+
|| '''Slide 8'''
  
<div style="color:#000000;">'''Drop trait'''</div>
+
'''Drop trait'''
 
||
 
||
* <div style="color:#000000;margin-left:1.27cm;margin-right:0cm;">To understand the ownership rules, we need to note two traits: </div>
+
* To understand the ownership rules, we need to note two traits:  
*<span style="color:#000000;">'''drop '''</span><span style="color:#000000;">and</span><span style="color:#000000;">''' copy'''</span>
+
* '''drop '''and''' copy'''
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="color:#000000;">The</span>se traits are <span style="color:#000000;">fundamental traits related to resource management and memory handling.</span></div>
+
* These traits are fundamental traits related to resource management and memory handling.
  
|- 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:#000000;">Let us </span>understand the '''drop'''<span style="color:#000000;"> trait through an example.</span>
+
|| Let us understand the '''drop''' trait through 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>
+
|| Open'''Visual''''''code editor'''
|| <span style="color:#000000;">Open the </span><span style="color:#000000;">'''Visual </span>S<span style="color:#000000;">tudio code editor.'''</span>
+
|| Open the'''Visual Studio code editor.'''
|- 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>
+
|| In the menu bar, click on'''Terminal''' and select''' New Terminal'''.
  
|| <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>
+
|| 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;"
+
|-  
|| <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>
+
|| '''> cd''''''Desktop'''/'''MyRustProject'''
  
<span style="color:#000000;">'''>'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''cargo new </span>Ownership'''
+
'''>''''''cargo newOwnership'''
  
<div style="color:#000000;">'''In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> Ownership'''</div>
+
'''In the menu bar, File >> Open folder >> Desktop
|| <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 Ownership</span> '''<span style="color:#000000;">and press </span><span style="color:#000000;">'''Enter.'''</span>
+
>> MyRustProject >> Ownership'''
 +
|| Let us go to our working directory'''MyRustProject''' as explained earlier.
  
<div style="color:#000000;">Open the created project as shown.</div>
+
Type the command '''cargo new Ownership ''' and press'''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| <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>
+
Open the created project as shown.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">struct MyStruct {</div>
+
|| Point to the'''main.rs''' file.
 +
|| In the'''main.rs '''file, copy and paste the code from the'''Code file'''.
 +
|-  
 +
|| struct MyStruct {
  
<div style="color:#000000;">data: String,</div>
+
data: String,
  
<div style="color:#000000;">}</div>
+
}
  
<span style="color:#000000;">//highlight impl Drop for MyStruct</span><span style="color:#000000;">''' '''</span>
+
//highlight impl Drop for MyStruct''' '''
  
<div style="color:#000000;">impl Drop for MyStruct {</div>
+
impl Drop for MyStruct {
  
<div style="color:#000000;">fn drop(&mut self) {</div>
+
fn drop(&mut self) {
  
<div style="color:#000000;">println!("Dropping MyStruct with data: {}", self.data);</div>
+
println!("Dropping MyStruct with data: {}", self.data);
  
<div style="color:#000000;">}</div>
+
}
  
<div style="color:#000000;">}</div>
+
}
  
<div style="color:#000000;">fn main() {</div>
+
fn main() {
  
<div style="color:#000000;">println!("Start");</div>
+
println!("Start");
  
<div style="color:#000000;">{</div>
+
{
  
<div style="color:#000000;">let new_struct = MyStruct {</div>
+
let new_struct = MyStruct {
  
<div style="color:#000000;">data: String::from("Hello, world!"),</div>
+
data: String::from("Hello, world!"),
  
<div style="color:#000000;">};</div>
+
};
  
<div style="color:#000000;">}</div>
+
}
  
<div style="color:#000000;">println!("End");</div>
+
println!("End");
  
<div style="color:#000000;">// my_struct goes out of scope here, and the drop method is called.</div>
+
// my_struct goes out of scope here, and the drop method is called.
  
<div style="color:#000000;">}</div>
+
}
 
|| This code explains how '''ownership '''and''' Drop trait''' work together in Rust.
 
|| This code explains how '''ownership '''and''' Drop trait''' work together in Rust.
  
<span style="background-color:#ffffff;color:#000000;">Here we </span><span style="color:#000000;">created a </span><span style="color:#000000;">'''struct'''</span><span style="color:#000000;"> </span><span style="color:#000000;">'''MyStruct'''</span><span style="color:#000000;"> which contains data of </span><span style="color:#000000;">'''string'''</span><span style="color:#000000;"> type</span>.
+
Here wecreated a'''struct''''''MyStruct''' which contains data of'''string''' type.
  
<span style="color:#000000;">In the implementation block, </span>the''' drop'''<span style="color:#000000;"> trait for </span><span style="color:#000000;">'''Mystruct'''</span><span style="color:#000000;"> has been implemented.</span>
+
In the implementation block,the''' drop''' trait for'''Mystruct''' has been implemented.
  
<span style="color:#000000;">The drop method is automatically called when an object </span><span style="color:#000000;">goes out of scope.</span>
+
The '''drop''' method is automatically called when an object goes out of scope.
  
<span style="color:#000000;">In the main program, </span><span style="color:#000000;">'''new_struct'''</span><span style="color:#000000;"> is created with "</span><span style="color:#000000;">'''Hello, world!"'''</span><span style="color:#000000;"> stored in its data.</span>
+
In the main program,'''new_struct''' is created with "'''Hello, world!"''' stored in its data.
  
<span style="color:#000000;">Here, </span><span style="color:#000000;">'''new_struct'''</span><span style="color:#000000;"> </span><span style="color:#000000;">owns </span><span style="color:#000000;">that string.</span>
+
Here,'''new_struct'''owns that string.
  
<span style="color:#000000;">At the end of the inner scope, </span><span style="color:#000000;">'''new_struct '''</span><span style="color:#000000;">goes out of scope.</span>
+
At the end of the inner scope,'''new_struct '''goes out of scope.
  
<span style="color:#000000;">Rust </span><span style="color:#000000;">automatically calls</span><span style="color:#000000;"> </span><span style="color:#000000;">'''drop(&mut self)'''</span><span style="color:#000000;"> for </span><span style="color:#000000;">'''new_struct.'''</span>
+
Rust automatically calls'''drop(&mut self)''' for'''new_struct.'''
  
 
In this code,''' drop''' happens before "End" is printed.
 
In this code,''' drop''' happens before "End" is printed.
Line 150: Line 151:
  
 
Save the program.
 
Save the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| 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;"
+
|-  
 
||  
 
||  
|| <span style="color:#000000;">Next, let us see an example of a </span><span style="color:#000000;">'''copy'''</span><span style="color:#000000;"> trait.</span>
+
|| Next, let us see an example of a '''copy''' trait.
  
<span style="color:#000000;">Clear the code window and copy paste the code from the </span>'''C<span style="color:#000000;">ode file'''</span><span style="color:#000000;">.</span>
+
Clear the code window and copy 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:#252525;">fn main() {</div>
+
|| fn main() {
  
<div style="color:#252525;">let x = 10;</div>
+
let x = 10;
  
<div style="color:#252525;">let y = x; // x is copied, not moved</div>
+
let y = x; // x is copied, not moved
  
<div style="color:#252525;">println!("x: {}, y: {}", x, y); // x is still accessible</div>
+
println!("x: {}, y: {}", x, y); // x is still accessible
  
<div style="color:#252525;">let s = String::from("Hello world");</div>
+
let s = String::from("Hello world");
  
<span style="color:#252525;">let </span><span style="color:#252525;">v</span><span style="color:#252525;"> = s; // Move occurs, `s` is no longer valid</span>
+
letv = s; // Move occurs, `s` is no longer valid
  
<div style="color:#252525;">// println!("{}", s); // This would cause an error</div>
+
// println!("{}", s); // This would cause an error
  
<div style="color:#252525;">}</div>
+
}
  
|| <div style="color:#252525;">Here, x is assigned a value 10, and y is assigned to variable x. </div>
+
|| Here, x is assigned a value 10, and y is assigned to variable x.  
  
<span style="color:#000000;">For simple types like i32, bool, char, etc., Rust </span><span style="color:#000000;">'''copies'''</span><span style="color:#000000;"> the value instead of moving it.</span>
+
For simple types like i32, boolean, char, etc., Rust'''copies''' the value instead of moving it.
  
<span style="color:#000000;">These data types are entirely stored on the </span><span style="color:#000000;">'''stack. '''</span>
+
These data types are entirely stored on the '''stack. '''
  
<div style="color:#000000;">It has a fixed size known at compile time.</div>
+
It has a fixed size known at compile time.
  
<span style="color:#000000;">After copying, both</span><span style="color:#000000;">''' x'''</span><span style="color:#000000;"> and</span><span style="color:#000000;">''' y '''</span><span style="color:#252525;">hold the value 10.</span>
+
After copying, both ''' x''' and''' y '''hold the value 10.
  
<span style="color:#252525;">But for the string, the variable </span><span style="color:#252525;">''s''</span><span style="color:#252525;"> is assigned the value “Hello World!”.</span>
+
But for the string, the variable '''s''' is assigned the value “Hello World!”.
  
 
Variable '''v '''is assigned to '''s.'''
 
Variable '''v '''is assigned to '''s.'''
  
<span style="color:#252525;">Here, </span><span style="color:#252525;">copy doesn’t happen because of </span><span style="color:#252525;">'''String'''</span><span style="color:#252525;"> being an owned type. </span>
+
Here,copy doesn’t happen because of '''String''' being an owned type.
  
<span style="color:#252525;">This fails because after the move, </span><span style="color:#252525;">'''s '''</span><span style="color:#252525;">no longer owns any valid data.</span>
+
This fails because after the move,'''s ''' no longer owns any valid data.
  
<span style="color:#000000;">Rust’s compiler prevents you from using </span><span style="color:#000000;">'''s '''</span><span style="color:#000000;">here.</span>
+
Rust’s compiler prevents you from using '''s ''' here.
  
<div style="color:#252525;">Hence it would show an error.</div>
+
Hence it would show an error.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
|| Type cargo run.
 
|| Run the code to see the output.
 
|| Run the code to see the output.
  
<span style="color:#000000;">We can see the copy trait is implemented by default for </span><span style="color:#000000;">'''integer'''</span><span style="color:#000000;">.</span>
+
We can see the copy trait is implemented by default for'''integer'''.
  
<span style="color:#000000;">For string type we </span>get<span style="color:#000000;"> an error.</span>
+
For string type we get an error.
  
<span style="color:#252525;">Types like </span><span style="color:#252525;">'''String'''</span><span style="color:#252525;"> or other heap-allocated structures do not implement </span><span style="color:#252525;">'''Copy'''</span><span style="color:#252525;"> by default.</span>
+
Types like'''String''' or other heap-allocated structures do not implement '''Copy''' by default.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <div style="color:#000000;">let t = s.clone(); // deep copy of the string data</div>
+
|| let t = s.clone(); // deep copy of the string data
  
<span style="color:#000000;">println!("s: {}, </span>v<span style="color:#000000;">: {}", s, </span>v<span style="color:#000000;">); // both accessible</span>
+
println!("s: {},v: {}", s,v); // both accessible
 
|| We can use the '''clone''' method to solve this issue.
 
|| We can use the '''clone''' method to solve this issue.
  
 
Let's add the clone method as '''s.clone() '''in the code.
 
Let's add the clone method as '''s.clone() '''in the code.
  
'''.clone() '''makes an '''explicit''' deep copy of the heap data.
+
'''.clone() '''method makes an '''explicit''' deep copy of the heap data.
  
Change the print statement to print '''s '''and''' v .'''
+
Change the print statement to print '''s '''and ''' v'''.
  
 
Save the program.
 
Save the program.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
| style="color:#000000;" |  
+
||  
 
|| Run the code to see the correct output.
 
|| Run the code to see the correct output.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>9'''
+
|| '''Slide 9'''
  
<span style="color:#000000;">'''Summary'''</span>
+
'''Summary'''
|| <div style="color:#000000;">This brings us to the end of this tutorial.</div>
+
|| This brings us to the end of this tutorial.
  
<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;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>10'''
+
|| '''Slide10'''
  
<div style="color:#000000;">'''Assignment'''</div>
+
'''Assignment'''
 
|| As an assignment. write a program that:
 
|| As an assignment. write a program that:
* <div style="margin-left:1.27cm;margin-right:0cm;">Creates a String variable called '''greeting''' with the value "Hello, Rust!".</div>
+
* Creates a String variable called '''greeting''' with the value "Hello, Rust!".
* <div style="margin-left:1.27cm;margin-right:0cm;">Passes it to a function '''takes_ownership''' that prints the value.</div>
+
* Passes it to a function '''takes_ownership''' that prints the value.
* <div style="margin-left:1.27cm;margin-right:0cm;">Inside this function, remember that ownership is moved.</div>
+
* Inside this function, remember that ownership is moved.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| <span style="color:#000000;">'''Slide </span>11'''
+
|| '''Slide11'''
  
<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>
 

Latest revision as of 15:28, 7 October 2025

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

Learning Objectives

In this tutorial, we will learn about:
  • Ownership
  • Ownership Rules
  • Drop and Copy traits
Slide 3

System Requirements

To record this tutorial I am using:
  • Ubuntu Linux OS version 22.04
  • Rust version 1.80.0
  • Visual Studio Code version 1.19.0
Slide 4

Prerequisites

To follow this tutorial, you should be familiar with compiling and running a Rust program.
Slide 5

Code Files

  • The following code file is required to practise this tutorial.
  • Ownership.rs
  • This file is provided in theCode Files link of this tutorial page.
Slide 6

Ownership

Let us now learn about Ownership.
  • Ownership allows memory safety without a garbage collector.
  • It is a key feature of Rust language that allows it to be both safe and fast.
Slide 7

Ownership Rules

  • Each value in Rust has a single owner
  • There can be only one owner at a time
  • When the owner goes out of scope the value will be dropped.
Slide 8

Drop trait

  • To understand the ownership rules, we need to note two traits:
  • drop and copy
  • These traits are fundamental traits related to resource management and memory handling.
Let us understand the drop trait through an example.
Open'Visual'code editor Open theVisual Studio code editor.
In the menu bar, click onTerminal and select New Terminal. In the menu bar, click onTerminal and select New Terminal.
> cd'Desktop'/MyRustProject

'>'cargo newOwnership

In the menu bar, File >> Open folder >> Desktop

>> MyRustProject >> Ownership

Let us go to our working directoryMyRustProject as explained earlier.

Type the command cargo new Ownership and pressEnter.

Open the created project as shown.

Point to themain.rs file. In themain.rs file, copy and paste the code from theCode file.
struct MyStruct {

data: String,

}

//highlight impl Drop for MyStruct

impl Drop for MyStruct {

fn drop(&mut self) {

println!("Dropping MyStruct with data: {}", self.data);

}

}

fn main() {

println!("Start");

{

let new_struct = MyStruct {

data: String::from("Hello, world!"),

};

}

println!("End");

// my_struct goes out of scope here, and the drop method is called.

}

This code explains how ownership and Drop trait work together in Rust.

Here wecreated a'struct'MyStruct which contains data ofstring type.

In the implementation block,the drop trait forMystruct has been implemented.

The drop method is automatically called when an object goes out of scope.

In the main program,new_struct is created with "Hello, world!" stored in its data.

Here,new_structowns that string.

At the end of the inner scope,new_struct goes out of scope.

Rust automatically callsdrop(&mut self) fornew_struct.

In this code, drop happens before "End" is printed.

This is because the struct’s scope ends earlier.

Save the program.

Type cargo run. Run the code to see the output.
Next, let us see an example of a copy trait.

Clear the code window and copy paste the code from theCode file.

fn main() {

let x = 10;

let y = x; // x is copied, not moved

println!("x: {}, y: {}", x, y); // x is still accessible

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

letv = s; // Move occurs, `s` is no longer valid

// println!("{}", s); // This would cause an error

}

Here, x is assigned a value 10, and y is assigned to variable x.

For simple types like i32, boolean, char, etc., Rustcopies the value instead of moving it.

These data types are entirely stored on the stack.

It has a fixed size known at compile time.

After copying, both x and y hold the value 10.

But for the string, the variable s is assigned the value “Hello World!”.

Variable v is assigned to s.

Here,copy doesn’t happen because of String being an owned type.

This fails because after the move,s no longer owns any valid data.

Rust’s compiler prevents you from using s here.

Hence it would show an error.

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

We can see the copy trait is implemented by default forinteger.

For string type we get an error.

Types likeString or other heap-allocated structures do not implement Copy by default.

let t = s.clone(); // deep copy of the string data

println!("s: {},v: {}", s,v); // both accessible

We can use the clone method to solve this issue.

Let's add the clone method as s.clone() in the code.

.clone() method makes an explicit deep copy of the heap data.

Change the print statement to print s and v.

Save the program.

Run the code to see the correct output.
Slide 9

Summary

This brings us to the end of this tutorial.

Let us summarize.

Slide10

Assignment

As an assignment. write a program that:
  • Creates a String variable called greeting with the value "Hello, Rust!".
  • Passes it to a function takes_ownership that prints the value.
  • Inside this function, remember that ownership is moved.
Slide11

Thank You

Thank you for joining

Contributors and Content Editors

Madhurig, Nirmala Venkat