Skip to content

Commit

Permalink
new challenge ownership
Browse files Browse the repository at this point in the history
challenges.json re-order
  • Loading branch information
dcodesdev committed Jun 13, 2024
1 parent 6451a22 commit b4d2cc5
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 48 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 60 additions & 48 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,66 @@
"created_at": "2024-06-12T00:00:00Z",
"updated_at": "2024-06-12T00:00:00Z"
},
{
"id": 10,
"title": "Basic If-Else Statements",
"slug": "if-else",
"short_description": "Learn how to use if-else statements in Rust to control the flow of your program.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["control flow", "if-else"],
"created_at": "2024-06-01T00:00:00Z",
"updated_at": "2024-06-01T00:00:00Z"
},
{
"id": 11,
"title": "Sum of Even Numbers",
"slug": "sum-of-even-numbers",
"short_description": "Implement a function to sum even numbers in a given range using a for loop.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["for-loop", "iteration", "range"],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 12,
"title": "Countdown",
"slug": "countdown",
"short_description": "Implement a countdown timer using a while loop in Rust.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["while", "loop", "control flow"],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 13,
"title": "Weekday from Number",
"slug": "weekday-from-number",
"short_description": "Use Rust's pattern matching to determine the weekday given its number.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["pattern matching", "match", "control flow"],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 34,
"title": "Ownership",
"slug": "ownership",
"short_description": "Understand and implement Rust's ownership model.",
"language": "RUST",
"difficulty": "EASY",
"track": "RUST_BASICS",
"tags": ["ownership", "borrowing", "lifetimes"],
"created_at": "2024-06-13T00:00:00Z",
"updated_at": "2024-06-13T00:00:00Z"
},
{
"id": 2,
"title": "Character counting string",
Expand Down Expand Up @@ -251,54 +311,6 @@
"created_at": "2024-05-06T00:00:00Z",
"updated_at": "2024-05-06T00:00:00Z"
},
{
"id": 10,
"title": "Basic If-Else Statements",
"slug": "if-else",
"short_description": "Learn how to use if-else statements in Rust to control the flow of your program.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["control flow", "if-else"],
"created_at": "2024-06-01T00:00:00Z",
"updated_at": "2024-06-01T00:00:00Z"
},
{
"id": 11,
"title": "Sum of Even Numbers",
"slug": "sum-of-even-numbers",
"short_description": "Implement a function to sum even numbers in a given range using a for loop.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["for-loop", "iteration", "range"],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 12,
"title": "Countdown",
"slug": "countdown",
"short_description": "Implement a countdown timer using a while loop in Rust.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": ["while", "loop", "control flow"],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 13,
"title": "Weekday from Number",
"slug": "weekday-from-number",
"short_description": "Use Rust's pattern matching to determine the weekday given its number.",
"language": "RUST",
"difficulty": "EASY",
"track": "CONTROL_FLOW",
"tags": [],
"created_at": "2024-06-02T00:00:00Z",
"updated_at": "2024-06-02T00:00:00Z"
},
{
"id": 14,
"title": "Is Prime",
Expand Down
6 changes: 6 additions & 0 deletions challenges/ownership/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ownership"
version = "0.1.0"
edition = "2021"

[dependencies]
62 changes: 62 additions & 0 deletions challenges/ownership/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Rust's ownership model is one of its most unique and powerful features, ensuring **memory safety** without needing a garbage collector. Ownership in Rust is governed by a set of rules that the compiler checks at compile time. Understanding these rules is crucial for writing efficient and safe Rust code.

## Ownership Basics

In Rust, each value has a variable that's called its **owner**. There can only be one owner at a time, and when the owner goes out of scope, the value is dropped. Here are the basic rules of ownership:

1. Each value in Rust has a variable that's called its **owner**.
2. There can only be **one owner at a time**.
3. When the owner goes out of scope, the value will be **dropped** (no longer valid).

### Example

```rust
{
let s = String::from("hello"); // s is the owner of the String
} // s goes out of scope and "hello" is dropped
```

## Borrowing

Rust allows you to create **references** to a value, which lets you access it without taking ownership. This is called **borrowing**. Borrowing can be **immutable** or **mutable**.

### Immutable References

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

```rust
fn main() {
let s1 = String::from("hello");

let len = calculate_length(&s1); // borrow s1 as immutable
println!("The length of '{}' is {}.", s1, len);
}

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
```

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

The task is designed to help you understand the concepts of ownership and immutable borrowing in Rust.

## Requirements

- The `calculate_length` function should take **an immutable reference** to the input `String` and return its length.

## Example

```rust
let s1 = String::from("hello");
let len = calculate_length(&s1);
assert_eq!(len, 5);
```

## Hints

- The `String` type in Rust has a method `len()` which returns its length.
3 changes: 3 additions & 0 deletions challenges/ownership/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn calculate_length(s: &String) -> usize {
s.len()
}
3 changes: 3 additions & 0 deletions challenges/ownership/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn calculate_length(s: &String) -> usize {
// Your code here...
}
37 changes: 37 additions & 0 deletions challenges/ownership/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[cfg(test)]
mod tests {
use ownership::calculate_length;

#[test]
fn test_calculate_length() {
assert_eq!(
calculate_length(&String::from("hello")),
5,
"Expected the length of 'hello' to be 5"
);

assert_eq!(
calculate_length(&String::from("world")),
5,
"Expected the length of 'world' to be 5"
);

assert_eq!(
calculate_length(&String::from("hello world")),
11,
"Expected the length of 'hello world' to be 11"
);

assert_eq!(
calculate_length(&String::from("hello world!")),
12,
"Expected the length of 'hello world!' to be 12"
);

assert_eq!(
calculate_length(&String::from("I'm a rust developer, and I love it!")),
36,
"Expected the text to be 39 characters long"
);
}
}

0 comments on commit b4d2cc5

Please sign in to comment.