From b95eba343ea87d199e9925e3cec9de9fa68794bf Mon Sep 17 00:00:00 2001 From: Savi Dahegaonkar <124272050+SaviDahegaonkar@users.noreply.github.com> Date: Wed, 8 Jan 2025 17:09:25 +0530 Subject: [PATCH] [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 --------- --- content/rust/concepts/ownership/ownership.md | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 content/rust/concepts/ownership/ownership.md diff --git a/content/rust/concepts/ownership/ownership.md b/content/rust/concepts/ownership/ownership.md new file mode 100644 index 00000000000..ca691a6b799 --- /dev/null +++ b/content/rust/concepts/ownership/ownership.md @@ -0,0 +1,78 @@ +--- +Title: 'Ownership' +Description: 'A compile-time system enforcing strict memory safety rules.' +Subjects: + - 'Code Foundations' + - 'Computer Science' + - 'Developer Tools' +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. + +## 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, Welcome to Codecademy! +} + +fn take_ownership(s: String) -> String { // Function takes ownership of the passed value + println!("Taking ownership: {}", s); // Prints: Taking ownership: Hello, Welcome to Codecademy! + 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.