-
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.
New challenge: Primitive data types (#18)
- Loading branch information
Showing
9 changed files
with
144 additions
and
2 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,7 @@ | ||
[package] | ||
name = "primitive-data-types" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dev-dependencies] | ||
syntest = { version = "0.1.0", path = "../../crates/syntest" } |
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,29 @@ | ||
Rust is a **statically-typed** language, which means that every variable must have a specific type. Rust's type system is designed to be safe and to prevent many common errors that occur in other languages. In this challenge, you will learn about some of the basic primitive data types in Rust, such as integers, floating-point numbers, booleans, and characters. | ||
|
||
Understanding how to declare and use these basic data types is fundamental to writing effective Rust code. This challenge will guide you through defining variables with specific types and initializing them. | ||
|
||
## Your task | ||
|
||
1. Define variable `x` with type `u8` | ||
2. Define variable `y` with type `f64` | ||
3. Define variable `z` with type `bool` | ||
4. Define variable `a` with type `char` | ||
|
||
Each of these variables **should be annotated** with their respective types and initialized with specific **values of your choice**. | ||
|
||
## Requirements | ||
|
||
- Every variable **must be annotated** with its type. | ||
- Initialize each variable with any value of your choice of the correct type. | ||
|
||
## Explanation of Data Types | ||
|
||
- **Integer** `(u8)`: Represents an `8-bit` unsigned integer. | ||
- **Floating-point number** `(f64)`: Represents a `64-bit` **floating-point number**. | ||
- **Boolean** `(bool)`: Represents a `boolean` value, which can be either `true` or `false`. | ||
- **Character** `(char)`: Represents a single Unicode **scalar** value. | ||
|
||
## Hints | ||
|
||
- Use the `let` keyword to define a variable. | ||
- Annotate the variable's type by specifying `let variable_name: type = `. |
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,8 @@ | ||
#[allow(warnings)] | ||
|
||
pub fn data_types() { | ||
let x: u8 = 42; | ||
let y: f64 = 3.224242422424; | ||
let z: bool = false; | ||
let a: char = '7'; | ||
} |
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,9 @@ | ||
pub fn data_types() { | ||
// 1. Define variable `x` of type `u8` | ||
|
||
// 2. Define variable `y` of type `f64` | ||
|
||
// 3. Define variable `z` of type `bool` | ||
|
||
// 4. Define variable `a` of type `char` | ||
} |
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,51 @@ | ||
#[cfg(test)] | ||
|
||
mod tests { | ||
use primitive_data_types::*; | ||
use syntest::{parse_quote, Pat, Stmt, Syntest}; | ||
|
||
#[test] | ||
fn test_compiles() { | ||
data_types(); | ||
} | ||
|
||
#[test] | ||
fn test_annotations() { | ||
let syntest = Syntest::from("./src/lib.rs"); | ||
|
||
let pats_actual = syntest.get_pats("data_types"); | ||
let stmts_expected: [Stmt; 4] = [ | ||
parse_quote! { | ||
let x: u8 = 42; | ||
}, | ||
parse_quote! { | ||
let y: f64 = 3.14; | ||
}, | ||
parse_quote! { | ||
let z: bool = true; | ||
}, | ||
parse_quote! { | ||
let a: char = 'R'; | ||
}, | ||
]; | ||
|
||
let mut expected_count = 0; | ||
for (pat_actual, stmt_expected) in pats_actual.iter().zip(stmts_expected.iter()) { | ||
if let Stmt::Local(local_expected) = stmt_expected { | ||
if let Pat::Type(type_expected) = &local_expected.pat { | ||
if let Pat::Ident(ident_expected) = &*type_expected.pat { | ||
let pat_expected = &local_expected.pat; | ||
assert_eq!( | ||
pat_actual, pat_expected, | ||
"Wrong type annotated for {}", | ||
ident_expected.ident | ||
); | ||
expected_count += 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
assert_eq!(expected_count, stmts_expected.len()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
pub use syn::parse_quote; | ||
pub use syn::Type; | ||
pub use syn::*; |
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