Skip to content

Commit

Permalink
description update
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 14, 2024
1 parent 28739c3 commit 597c3b4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions challenges/ownership/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Rust allows you to create **references** to a value, which lets you access it wi

You can create multiple **immutable references** to a value, but you cannot have a mutable reference while immutable references exist. This allows you to read from the value without changing it.

## Example
### Example

```rust
fn main() {
Expand All @@ -36,9 +36,15 @@ fn main() {

fn calculate_length(s: &String) -> usize { // s is an immutable reference to a String
s.len()
} // s goes out of scope here, but since it does not have ownership of the String, nothing happens
}
```

## Using `&` to Create References

In the example above, `&s1` creates an immutable reference to `s1`. This means that `calculate_length` borrows `s1` but does not take ownership of it. The `&` symbol is used to denote a reference in Rust. This allows the function to access the value without taking ownership, which means `s1` can still be used after the function call.

Similarly, in the function signature `fn calculate_length(s: &String) -> usize`, `&String` indicates that the parameter `s` is an immutable reference to a `String`. This allows the function to read from the `String` without modifying it or taking ownership.

## Challenge

In this challenge, you will create a function `calculate_length` that takes an immutable reference to a `String`, calculates its length, and returns the length.
Expand Down

0 comments on commit 597c3b4

Please sign in to comment.