Skip to content

Commit

Permalink
Structure Ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
ramagururadhakrishnan authored May 7, 2024
1 parent 2efd78e commit c6101c9
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Assets/Lectures/RL4.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ fn main() {
println!("Employee ID: {}, Name: {}, Department: {}", employee.id, employee.name, employee.department);
}
```
```
struct Point {
x: i32,
y: i32,
}
fn print_point(point: Point) {
println!("Point coordinates: ({}, {})", point.x, point.y);
}
fn print_point1(point: &Point) {
println!("Point coordinates: ({}, {})", point.x, point.y);
}
fn main() {
// Create a Point instance
let p = Point { x: 44, y: 18 };
// Pass the Point instance by reference to the print_point function
print_point(p);
// We cannot use the Point instance after transferring the ownership
//println!("Point coordinates after transfer: ({}, {})", p.x, p.y);
// Create a Point instance
let p = Point { x: 44, y: 18 };
// Pass the Point instance by reference to the print_point function
print_point1(&p);
// We can still use the Point instance after borrowing it
println!("Point coordinates after borrowing: ({}, {})", p.x, p.y);
}
```

### Modules

Expand Down

0 comments on commit c6101c9

Please sign in to comment.