Difference between revisions of "Rust-Programming-Language/C2/Ownership/English"
(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...") |
(No difference)
|
Revision as of 17:42, 22 September 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:
|
| Slide 3
System Requirements
|
To record this tutorial I am using:
|
| Slide 4
Prerequisites
|
To follow this tutorial, you should be familiar with compiling and running a Rust program. |
| Slide 5
Code Files
|
|
| Slide 6
Ownership
|
Let us now learn about Ownership.
|
| Slide 7
Ownership Rules
|
|
| Slide 8
Drop trait
|
|
| Let us understand the drop trait through 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 Ownership In the menu bar, File >> Open folder >> Desktop >> MyRustProject >> Ownership
|
Let us go to our working directory MyRustProject as explained earlier.
Type the command cargo new Ownership 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. |
| 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 we created a struct MyStruct which contains data of string type. In the implementation block, the drop trait for Mystruct 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_struct owns that string. At the end of the inner scope, new_struct goes out of scope. Rust automatically calls drop(&mut self) for new_struct. In this code, drop happens before "End" is printed. This is because the struct’s scope ends earlier. Save the program. |
| 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 the Code 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");
let v = 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, bool, char, etc., Rust copies 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.
|
| Run the code to see the output.
We can see the copy trait is implemented by default for integer. For string type we get an error. Types like String 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() 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.
|
| Slide 10
Assignment
|
As an assignment. write a program that:
|
| Slide 11
Thank You
|
Thank you for joining |