diff --git a/Cargo.lock b/Cargo.lock index 412d141..b04294d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,6 +227,13 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "constants" +version = "0.1.0" +dependencies = [ + "syntest", +] + [[package]] name = "core-foundation" version = "0.9.4" diff --git a/challenges/challenges.json b/challenges/challenges.json index ac54500..42ce0a6 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -7,7 +7,7 @@ "language": "RUST", "difficulty": "BEGINNER", "track": "RUST_BASICS", - "tags": [], + "tags": ["function", "string", "output"], "created_at": "2024-04-20T00:00:00Z", "updated_at": "2024-04-20T00:00:00Z" }, @@ -29,12 +29,24 @@ "slug": "mutable-variables", "short_description": "Define and modify mutable variables in Rust.", "language": "RUST", - "difficulty": "EASY", + "difficulty": "BEGINNER", "track": "RUST_BASICS", - "tags": [], + "tags": ["variables", "mutability"], "created_at": "2024-06-08T00:00:00Z", "updated_at": "2024-06-08T00:00:00Z" }, + { + "id": 24, + "title": "Constants", + "slug": "constants", + "short_description": "Learn how to define and use constants in Rust.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": ["constants", "definition"], + "created_at": "2024-06-09T00:00:00Z", + "updated_at": "2024-06-09T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/constants/Cargo.toml b/challenges/constants/Cargo.toml new file mode 100644 index 0000000..d276ed1 --- /dev/null +++ b/challenges/constants/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "constants" +version = "0.1.0" +edition = "2021" + +[dev-dependencies] +syntest = { path = "../../crates/syntest" } diff --git a/challenges/constants/src/lib.rs b/challenges/constants/src/lib.rs new file mode 100644 index 0000000..2bcd954 --- /dev/null +++ b/challenges/constants/src/lib.rs @@ -0,0 +1,5 @@ +const MAX_SIZE: i32 = 100; + +pub fn main() -> i32 { + MAX_SIZE +} diff --git a/challenges/constants/src/starter.rs b/challenges/constants/src/starter.rs new file mode 100644 index 0000000..96d30d7 --- /dev/null +++ b/challenges/constants/src/starter.rs @@ -0,0 +1,6 @@ +// Define a constant MAX_SIZE with a value of 100. +// Your code here + +pub fn main() -> i32 { + MAX_SIZE +} diff --git a/challenges/constants/tests/tests.rs b/challenges/constants/tests/tests.rs new file mode 100644 index 0000000..efee0c2 --- /dev/null +++ b/challenges/constants/tests/tests.rs @@ -0,0 +1,42 @@ +#[cfg(test)] +mod tests { + use constants::*; + use syntest::Syntest; + + #[test] + fn compiles() { + assert_eq!(main(), 100, "Expected main() to return 100"); + } + + #[test] + fn test_constants() { + let syntest = Syntest::from("./src/lib.rs"); + + let const_to_exist = ["MAX_SIZE"]; + let constants = syntest.constant.constants(); + + let mut success = false; + for const_name in const_to_exist.iter() { + constants.iter().for_each(|con| { + if con.ident.to_string() != *const_name { + return; + } + + assert_eq!( + con.ty, + syntest::parse_quote! { i32 }, + "Expected type i32 for constant MAX_SIZE" + ); + assert_eq!( + con.expr, + syntest::parse_quote! { 100 }, + "Expected value 100 for constant MAX_SIZE" + ); + + success = true; + }) + } + + assert!(success, "Expected constant MAX_SIZE to exist"); + } +}