Skip to content

Commit

Permalink
the unit type
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 10, 2024
1 parent 5124917 commit 8999756
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
7 changes: 7 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 @@ -107,6 +107,18 @@
"created_at": "2024-06-10T00:00:00Z",
"updated_at": "2024-06-10T00:00:00Z"
},
{
"id": 30,
"title": "The Unit Type",
"slug": "the-unit-type",
"short_description": "Understand and use the unit type `()` in Rust.",
"language": "RUST",
"difficulty": "BEGINNER",
"track": "RUST_BASICS",
"tags": ["unit type", "basic operations"],
"created_at": "2024-06-10T00:00:00Z",
"updated_at": "2024-06-10T00:00:00Z"
},
{
"id": 2,
"title": "Character counting string",
Expand Down
7 changes: 7 additions & 0 deletions challenges/the-unit-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "the-unit-type"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
syntest = { path = "../../crates/syntest" }
19 changes: 19 additions & 0 deletions challenges/the-unit-type/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
In Rust, the unit type `()` is a type that has exactly one value, also written as `()`. It is used to indicate the absence of a meaningful value and is often seen in functions that do not return a value.

In this challenge, you will implement a function that prints a message and returns the unit type.

## Your task

You need to implement the function `print_message() -> ()` that prints `"Hello, Rust!"` to the console and returns the unit type.

## Requirements

- The `print_message` function should print `"Hello, Rust!"` to the console.
- The function should return the unit type `()`.

## Example

```rust
let result = print_message();
assert_eq!(result, ());
```
3 changes: 3 additions & 0 deletions challenges/the-unit-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print_message() -> () {
println!("Hello, Rust!");
}
3 changes: 3 additions & 0 deletions challenges/the-unit-type/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print_message() -> () {
// TODO: Implement the function here
}
34 changes: 34 additions & 0 deletions challenges/the-unit-type/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[cfg(test)]
mod tests {
use syntest::Syntest;
use the_unit_type::*;

#[test]
fn test_compiles() {
print_message();
}

#[test]
fn test_prints() {
let syntest = Syntest::new("print_message", "src/lib.rs");

let macros = syntest.mac.macros();

assert!(!macros.is_empty(), "You should use the `println!` macro");
assert_eq!(macros.len(), 1, "You should only print one message");

let macro_name = &macros[0].name;

assert_eq!(macro_name, "println", "You should use the `println!` macro");

for token in macros[0].tokens.iter() {
let token = token.to_lowercase();

assert_eq!(
(token.contains("hello"), token.contains("rust")),
(true, true),
"You should print `Hello, Rust!`"
)
}
}
}

0 comments on commit 8999756

Please sign in to comment.