Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concept Entry] Rust: Ownership #5880

Merged
merged 23 commits into from
Jan 8, 2025
Merged
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3d161b7
New file has been added.
SaviDahegaonkar Aug 6, 2024
5a211ba
Update user-input.md
cigar-galaxy82 Sep 8, 2024
38a6c7b
Merge branch 'main' into main
cigar-galaxy82 Sep 8, 2024
5923e4e
Update user-input.md
cigar-galaxy82 Sep 8, 2024
3e57a05
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Oct 16, 2024
7fd94ae
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Oct 17, 2024
ba4692a
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Oct 17, 2024
ea09cd7
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Nov 19, 2024
8ef7979
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 2, 2024
4c5cfc5
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 3, 2024
80df0b6
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 7, 2024
fbcbbfb
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 13, 2024
5787bae
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 18, 2024
492d478
Merge branch 'Codecademy:main' into main
SaviDahegaonkar Dec 19, 2024
4e5be79
File has been modified.
SaviDahegaonkar Dec 20, 2024
dc2c937
Merge branch 'main' into ownership
Radhika-okhade Jan 8, 2025
0fac7ee
Update content/rust/concepts/ownership/ownwership.md
Radhika-okhade Jan 8, 2025
d616204
Update content/rust/concepts/ownership/ownwership.md
Radhika-okhade Jan 8, 2025
4908ab9
Update content/rust/concepts/ownership/ownwership.md
Radhika-okhade Jan 8, 2025
5f7e166
Rename ownwership.md to ownership.md
Radhika-okhade Jan 8, 2025
7135e95
Update content/rust/concepts/ownership/ownership.md
Radhika-okhade Jan 8, 2025
f284a70
Update ownership.md
Radhika-okhade Jan 8, 2025
93ce316
Update ownership.md
Radhika-okhade Jan 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions content/rust/concepts/ownership/ownwership.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
Title: 'Ownership'
Description: 'A compile-time system enforcing strict memory safety rules, preventing errors like dangling pointers and double-free issues.'
Radhika-okhade marked this conversation as resolved.
Show resolved Hide resolved
Subjects:
- 'Computer Science'
- 'Code Foundations'
- 'Developer Tools'
Radhika-okhade marked this conversation as resolved.
Show resolved Hide resolved
Tags:
- 'Alias'
- 'Const'
- 'Memory'
CatalogContent:
- 'rust-for-programmers'
- 'paths/computer-science'
---

**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.
Radhika-okhade marked this conversation as resolved.
Show resolved Hide resolved

## Syntax

Here's the syntax for the ownership concept in Rust:

```pseudo
let variable1 = value;
let variable2 = variable1;
// 'variable1' is no longer valid and cannot be accessed.
```

- `variable1`: First owner of the value.
- `value`: The value or data to be owned initially.
- `variable2`: New owner of `value` after the ownership transfer.

## Example

This example demonstrates how ownership works, ensuring that only one variable owns the data at a time:

```rust
fn main() {
let s1 = String::from("Hello, Welcome to Codecademy!");
println!("s1: {}", s1);
let s2 = take_ownership(s1);
// println!("s1: {}", s1); // Uncommenting this line would result in a compile-time error because 's1' no longer owns the value.
println!("s2: {}", s2); // Prints: s2: Hello, Rust!
}

fn take_ownership(s: String) -> String { // Function takes ownership of the passed value
println!("Taking ownership: {}", s); // Prints: Taking ownership: Hello, Rust!
s // Ownership is returned to the caller, moving it back to the caller.
}
```

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!":

The example will result in the following output:

```shell
s1: Hello, Welcome to Codecademy!
Taking ownership: Hello, Welcome to Codecademy!
s2: Hello, Welcome to Codecademy!
```

### Unsafe Code Example

Rust’s ownership system prevents operations that could lead to unsafe memory access, like accessing data after ownership is moved, at compile time :

```rust
fn main() {
let s1 = String::from("Hello");
let s2 = take_ownership(s1);
// println!("{}", s1); // Uncommenting this line would cause a compile-time error: use of moved value
}

fn take_ownership(s: String) -> String {
s
}
```

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.
Loading