-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "tuples" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
} | ||
} |