Skip to content

Commit

Permalink
tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 10, 2024
1 parent 4baf3b1 commit 5124917
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 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.

12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
"created_at": "2024-06-10T00:00:00Z",
"updated_at": "2024-06-10T00:00:00Z"
},
{
"id": 29,
"title": "Tuples",
"slug": "tuples",
"short_description": "Create a function that returns a tuple of values.",
"language": "RUST",
"difficulty": "BEGINNER",
"track": "RUST_BASICS",
"tags": ["tuples", "basic operations"],
"created_at": "2024-06-10T00:00:00Z",
"updated_at": "2024-06-10T00:00:00Z"
},
{
"id": 2,
"title": "Character counting string",
Expand Down
6 changes: 6 additions & 0 deletions challenges/tuples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "tuples"
version = "0.1.0"
edition = "2021"

[dependencies]
24 changes: 24 additions & 0 deletions challenges/tuples/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Tuples are a simple and versatile data structure in Rust that allow you to **group multiple values of different types into a single compound value.** They are particularly useful for returning multiple values from a function.

**Tuples** can return multiple values of different types, which is not possible with arrays or slices. For example a tuple could be `(i32, f64, String)` which contains an integer, a float, and a string.

In this challenge, you will implement a function that takes three arguments of different types and returns them as a tuple.

## Your task

You need to implement the function `create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String)` that takes an **integer `i32`**, a **float `f64`**, and a **string slice `&str`** as input and returns them as a tuple. The string slice should be converted into a `String` type.

- The `create_tuple` function should return a tuple containing the three input values **in order**.
- The string slice input should be converted into a `String` before returning.

## Example

```rust
let result = create_tuple(42, 3.14, "hello");
assert_eq!(result, (42, 3.14, String::from("hello")));
```

## Hints

- Use parentheses `()` to define and return the tuple.
- Remember to convert the **string slice `&str`** to String using `String::from()` or the `.to_string()` method.
3 changes: 3 additions & 0 deletions challenges/tuples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) {
(a, b, c.to_string())
}
3 changes: 3 additions & 0 deletions challenges/tuples/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) {
// TODO: Implement the function here
}
22 changes: 22 additions & 0 deletions challenges/tuples/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[cfg(test)]
mod tests {
use tuples::*;

#[test]
fn test_create_tuple() {
let result = create_tuple(42, 3.14, "hello");
assert_eq!(result, (42, 3.14, String::from("hello")));
}

#[test]
fn test_create_tuple_empty_string() {
let result = create_tuple(0, 0.0, "");
assert_eq!(result, (0, 0.0, String::from("")));
}

#[test]
fn test_create_tuple_negative_values() {
let result = create_tuple(-42, -3.14, "negative");
assert_eq!(result, (-42, -3.14, String::from("negative")));
}
}

0 comments on commit 5124917

Please sign in to comment.