Skip to content

Commit 148c7d5

Browse files
[Concept Entry] Rust: Ownership (#5880)
* New file has been added. * Update user-input.md * Update user-input.md * File has been modified. * Update content/rust/concepts/ownership/ownwership.md * Update content/rust/concepts/ownership/ownwership.md * Update content/rust/concepts/ownership/ownwership.md * Rename ownwership.md to ownership.md fixed minor issues and corrected file name spelling * Update content/rust/concepts/ownership/ownership.md * Update ownership.md * Update ownership.md ---------
1 parent 7340800 commit 148c7d5

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
Title: 'Ownership'
3+
Description: 'A compile-time system enforcing strict memory safety rules.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
- 'Developer Tools'
8+
Tags:
9+
- 'Alias'
10+
- 'Const'
11+
- 'Memory'
12+
CatalogContent:
13+
- 'rust-for-programmers'
14+
- 'paths/computer-science'
15+
---
16+
17+
**Ownership** in Rust is one of the core memory management concepts that ensures every value has a single owner responsible for it. This system avoids memory leaks and data races without needing a garbage collector. Ownership features include move semantics, where the ownership is transferred between variables, borrowing, and the borrow checker, by which these rules are checked at compile time to keep memory safe.
18+
19+
## Syntax
20+
21+
Here's the syntax for the ownership concept in Rust:
22+
23+
```pseudo
24+
let variable1 = value;
25+
let variable2 = variable1;
26+
// 'variable1' is no longer valid and cannot be accessed.
27+
```
28+
29+
- `variable1`: First owner of the value.
30+
- `value`: The value or data to be owned initially.
31+
- `variable2`: New owner of `value` after the ownership transfer.
32+
33+
## Example
34+
35+
This example demonstrates how ownership works, ensuring that only one variable owns the data at a time:
36+
37+
```rust
38+
fn main() {
39+
let s1 = String::from("Hello, Welcome to Codecademy!");
40+
println!("s1: {}", s1);
41+
let s2 = take_ownership(s1);
42+
// println!("s1: {}", s1); // Uncommenting this line would result in a compile-time error because 's1' no longer owns the value.
43+
println!("s2: {}", s2); // Prints: s2: Hello, Welcome to Codecademy!
44+
}
45+
46+
fn take_ownership(s: String) -> String { // Function takes ownership of the passed value
47+
println!("Taking ownership: {}", s); // Prints: Taking ownership: Hello, Welcome to Codecademy!
48+
s // Ownership is returned to the caller, moving it back to the caller.
49+
}
50+
```
51+
52+
In this example, the ownership of the string s1 is moved to s2, and s1 is no longer valid. After moving, s2 holds the ownership of the string "Hello, Welcome to Codecademy!":
53+
54+
The example will result in the following output:
55+
56+
```shell
57+
s1: Hello, Welcome to Codecademy!
58+
Taking ownership: Hello, Welcome to Codecademy!
59+
s2: Hello, Welcome to Codecademy!
60+
```
61+
62+
### Unsafe Code Example
63+
64+
Rust’s ownership system prevents operations that could lead to unsafe memory access, like accessing data after ownership is moved, at compile time :
65+
66+
```rust
67+
fn main() {
68+
let s1 = String::from("Hello");
69+
let s2 = take_ownership(s1);
70+
// println!("{}", s1); // Uncommenting this line would cause a compile-time error: use of moved value
71+
}
72+
73+
fn take_ownership(s: String) -> String {
74+
s
75+
}
76+
```
77+
78+
In this unsafe scenario, the ownership of `s1` is moved to `s2`, and attempting to access `s1` after the move results in a compile-time error.

0 commit comments

Comments
 (0)