mirror of
https://github.com/ArthurDanjou/rustlings.git
synced 2026-02-04 21:57:49 +01:00
Add initial solution files for Rustlings exercises and quizzes
- Created empty solution files for various exercises in strings, modules, hashmaps, options, error handling, generics, traits, lifetimes, tests, iterators, smart pointers, threads, macros, clippy, conversions, and quizzes. - Each solution file contains a main function with a comment indicating that it will be automatically filled after completing the exercise. - Added a README.md file to provide information about the solutions and their purpose.
This commit is contained in:
5
solutions/00_intro/intro1.rs
Normal file
5
solutions/00_intro/intro1.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
// Congratulations, you finished the first exercise 🎉
|
||||
// As an introduction to Rustlings, the first exercise only required
|
||||
// entering `n` in the terminal to go to the next exercise.
|
||||
}
|
||||
4
solutions/00_intro/intro2.rs
Normal file
4
solutions/00_intro/intro2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/01_variables/variables1.rs
Normal file
4
solutions/01_variables/variables1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/01_variables/variables2.rs
Normal file
4
solutions/01_variables/variables2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/01_variables/variables3.rs
Normal file
4
solutions/01_variables/variables3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/01_variables/variables4.rs
Normal file
4
solutions/01_variables/variables4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
9
solutions/01_variables/variables5.rs
Normal file
9
solutions/01_variables/variables5.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
let number = "T-H-R-E-E";
|
||||
println!("Spell a number: {number}");
|
||||
|
||||
// Using variable shadowing
|
||||
// https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
|
||||
let number = 3;
|
||||
println!("Number plus two is: {}", number + 2);
|
||||
}
|
||||
4
solutions/01_variables/variables6.rs
Normal file
4
solutions/01_variables/variables6.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/02_functions/functions1.rs
Normal file
4
solutions/02_functions/functions1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/02_functions/functions2.rs
Normal file
4
solutions/02_functions/functions2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/02_functions/functions3.rs
Normal file
4
solutions/02_functions/functions3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/02_functions/functions4.rs
Normal file
4
solutions/02_functions/functions4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/02_functions/functions5.rs
Normal file
4
solutions/02_functions/functions5.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/03_if/if1.rs
Normal file
4
solutions/03_if/if1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/03_if/if2.rs
Normal file
4
solutions/03_if/if2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/03_if/if3.rs
Normal file
4
solutions/03_if/if3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/04_primitive_types/primitive_types1.rs
Normal file
4
solutions/04_primitive_types/primitive_types1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/04_primitive_types/primitive_types2.rs
Normal file
4
solutions/04_primitive_types/primitive_types2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/04_primitive_types/primitive_types3.rs
Normal file
4
solutions/04_primitive_types/primitive_types3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/04_primitive_types/primitive_types4.rs
Normal file
4
solutions/04_primitive_types/primitive_types4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/04_primitive_types/primitive_types5.rs
Normal file
4
solutions/04_primitive_types/primitive_types5.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/04_primitive_types/primitive_types6.rs
Normal file
4
solutions/04_primitive_types/primitive_types6.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/05_vecs/vecs1.rs
Normal file
4
solutions/05_vecs/vecs1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/05_vecs/vecs2.rs
Normal file
4
solutions/05_vecs/vecs2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/06_move_semantics/move_semantics1.rs
Normal file
4
solutions/06_move_semantics/move_semantics1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/06_move_semantics/move_semantics2.rs
Normal file
4
solutions/06_move_semantics/move_semantics2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/06_move_semantics/move_semantics3.rs
Normal file
4
solutions/06_move_semantics/move_semantics3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/06_move_semantics/move_semantics4.rs
Normal file
4
solutions/06_move_semantics/move_semantics4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/06_move_semantics/move_semantics5.rs
Normal file
4
solutions/06_move_semantics/move_semantics5.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/07_structs/structs1.rs
Normal file
4
solutions/07_structs/structs1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/07_structs/structs2.rs
Normal file
4
solutions/07_structs/structs2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/07_structs/structs3.rs
Normal file
4
solutions/07_structs/structs3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/08_enums/enums1.rs
Normal file
4
solutions/08_enums/enums1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/08_enums/enums2.rs
Normal file
4
solutions/08_enums/enums2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/08_enums/enums3.rs
Normal file
4
solutions/08_enums/enums3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/09_strings/strings1.rs
Normal file
4
solutions/09_strings/strings1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/09_strings/strings2.rs
Normal file
4
solutions/09_strings/strings2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/09_strings/strings3.rs
Normal file
4
solutions/09_strings/strings3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/09_strings/strings4.rs
Normal file
4
solutions/09_strings/strings4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/10_modules/modules1.rs
Normal file
4
solutions/10_modules/modules1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
23
solutions/10_modules/modules2.rs
Normal file
23
solutions/10_modules/modules2.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
mod delicious_snacks {
|
||||
// Added `pub` and used the expected alias after `as`.
|
||||
pub use self::fruits::PEAR as fruit;
|
||||
pub use self::veggies::CUCUMBER as veggie;
|
||||
|
||||
mod fruits {
|
||||
pub const PEAR: &str = "Pear";
|
||||
pub const APPLE: &str = "Apple";
|
||||
}
|
||||
|
||||
mod veggies {
|
||||
pub const CUCUMBER: &str = "Cucumber";
|
||||
pub const CARROT: &str = "Carrot";
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!(
|
||||
"favorite snacks: {} and {}",
|
||||
delicious_snacks::fruit,
|
||||
delicious_snacks::veggie,
|
||||
);
|
||||
}
|
||||
4
solutions/10_modules/modules3.rs
Normal file
4
solutions/10_modules/modules3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/11_hashmaps/hashmaps1.rs
Normal file
4
solutions/11_hashmaps/hashmaps1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/11_hashmaps/hashmaps2.rs
Normal file
4
solutions/11_hashmaps/hashmaps2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/11_hashmaps/hashmaps3.rs
Normal file
4
solutions/11_hashmaps/hashmaps3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/12_options/options1.rs
Normal file
4
solutions/12_options/options1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/12_options/options2.rs
Normal file
4
solutions/12_options/options2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
27
solutions/12_options/options3.rs
Normal file
27
solutions/12_options/options3.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
#[derive(Debug)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let optional_point = Some(Point { x: 100, y: 200 });
|
||||
|
||||
// Solution 1: Matching over the `Option` (not `&Option`) but without moving
|
||||
// out of the `Some` variant.
|
||||
match optional_point {
|
||||
Some(ref p) => println!("Coordinates are {},{}", p.x, p.y),
|
||||
// ^^^ added
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
|
||||
// Solution 2: Matching over a reference (`&Option`) by added `&` before
|
||||
// `optional_point`.
|
||||
match &optional_point {
|
||||
//^ added
|
||||
Some(p) => println!("Coordinates are {},{}", p.x, p.y),
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
|
||||
println!("{optional_point:?}");
|
||||
}
|
||||
37
solutions/13_error_handling/errors1.rs
Normal file
37
solutions/13_error_handling/errors1.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
fn generate_nametag_text(name: String) -> Result<String, String> {
|
||||
// ^^^^^^ ^^^^^^
|
||||
if name.is_empty() {
|
||||
// `Err(String)` instead of `None`.
|
||||
Err("Empty names aren't allowed".to_string())
|
||||
} else {
|
||||
// `Ok` instead of `Some`.
|
||||
Ok(format!("Hi! My name is {name}"))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn generates_nametag_text_for_a_nonempty_name() {
|
||||
assert_eq!(
|
||||
generate_nametag_text("Beyoncé".to_string()).as_deref(),
|
||||
Ok("Hi! My name is Beyoncé"),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explains_why_generating_nametag_text_fails() {
|
||||
assert_eq!(
|
||||
generate_nametag_text(String::new())
|
||||
.as_ref()
|
||||
.map_err(|e| e.as_str()),
|
||||
Err("Empty names aren't allowed"),
|
||||
);
|
||||
}
|
||||
}
|
||||
4
solutions/13_error_handling/errors2.rs
Normal file
4
solutions/13_error_handling/errors2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/13_error_handling/errors3.rs
Normal file
4
solutions/13_error_handling/errors3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/13_error_handling/errors4.rs
Normal file
4
solutions/13_error_handling/errors4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/13_error_handling/errors5.rs
Normal file
4
solutions/13_error_handling/errors5.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/13_error_handling/errors6.rs
Normal file
4
solutions/13_error_handling/errors6.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/14_generics/generics1.rs
Normal file
4
solutions/14_generics/generics1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/14_generics/generics2.rs
Normal file
4
solutions/14_generics/generics2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/15_traits/traits1.rs
Normal file
4
solutions/15_traits/traits1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/15_traits/traits2.rs
Normal file
4
solutions/15_traits/traits2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/15_traits/traits3.rs
Normal file
4
solutions/15_traits/traits3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
35
solutions/15_traits/traits4.rs
Normal file
35
solutions/15_traits/traits4.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
trait Licensed {
|
||||
fn licensing_info(&self) -> String {
|
||||
"Default license".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
struct SomeSoftware;
|
||||
struct OtherSoftware;
|
||||
|
||||
impl Licensed for SomeSoftware {}
|
||||
impl Licensed for OtherSoftware {}
|
||||
|
||||
fn compare_license_types(software1: impl Licensed, software2: impl Licensed) -> bool {
|
||||
// ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
|
||||
software1.licensing_info() == software2.licensing_info()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn compare_license_information() {
|
||||
assert!(compare_license_types(SomeSoftware, OtherSoftware));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compare_license_information_backwards() {
|
||||
assert!(compare_license_types(OtherSoftware, SomeSoftware));
|
||||
}
|
||||
}
|
||||
39
solutions/15_traits/traits5.rs
Normal file
39
solutions/15_traits/traits5.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
trait SomeTrait {
|
||||
fn some_function(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
trait OtherTrait {
|
||||
fn other_function(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
struct SomeStruct;
|
||||
impl SomeTrait for SomeStruct {}
|
||||
impl OtherTrait for SomeStruct {}
|
||||
|
||||
struct OtherStruct;
|
||||
impl SomeTrait for OtherStruct {}
|
||||
impl OtherTrait for OtherStruct {}
|
||||
|
||||
fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
|
||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
item.some_function() && item.other_function()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_some_func() {
|
||||
assert!(some_func(SomeStruct));
|
||||
assert!(some_func(OtherStruct));
|
||||
}
|
||||
}
|
||||
4
solutions/16_lifetimes/lifetimes1.rs
Normal file
4
solutions/16_lifetimes/lifetimes1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/16_lifetimes/lifetimes2.rs
Normal file
4
solutions/16_lifetimes/lifetimes2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/16_lifetimes/lifetimes3.rs
Normal file
4
solutions/16_lifetimes/lifetimes3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/17_tests/tests1.rs
Normal file
4
solutions/17_tests/tests1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/17_tests/tests2.rs
Normal file
4
solutions/17_tests/tests2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
45
solutions/17_tests/tests3.rs
Normal file
45
solutions/17_tests/tests3.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
struct Rectangle {
|
||||
width: i32,
|
||||
height: i32,
|
||||
}
|
||||
|
||||
impl Rectangle {
|
||||
// Don't change this function.
|
||||
fn new(width: i32, height: i32) -> Self {
|
||||
if width <= 0 || height <= 0 {
|
||||
// Returning a `Result` would be better here. But we want to learn
|
||||
// how to test functions that can panic.
|
||||
panic!("Rectangle width and height must be positive");
|
||||
}
|
||||
|
||||
Rectangle { width, height }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn correct_width_and_height() {
|
||||
let rect = Rectangle::new(10, 20);
|
||||
assert_eq!(rect.width, 10); // Check width
|
||||
assert_eq!(rect.height, 20); // Check height
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic] // Added this attribute to check that the test panics.
|
||||
fn negative_width() {
|
||||
let _rect = Rectangle::new(-10, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic] // Added this attribute to check that the test panics.
|
||||
fn negative_height() {
|
||||
let _rect = Rectangle::new(10, -10);
|
||||
}
|
||||
}
|
||||
4
solutions/18_iterators/iterators1.rs
Normal file
4
solutions/18_iterators/iterators1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
56
solutions/18_iterators/iterators2.rs
Normal file
56
solutions/18_iterators/iterators2.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
// In this exercise, you'll learn some of the unique advantages that iterators
|
||||
// can offer.
|
||||
|
||||
// "hello" -> "Hello"
|
||||
fn capitalize_first(input: &str) -> String {
|
||||
let mut chars = input.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first) => first.to_uppercase().to_string() + chars.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the `capitalize_first` function to a slice of string slices.
|
||||
// Return a vector of strings.
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
words.iter().map(|word| capitalize_first(word)).collect()
|
||||
}
|
||||
|
||||
// Apply the `capitalize_first` function again to a slice of string
|
||||
// slices. Return a single string.
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
fn capitalize_words_string(words: &[&str]) -> String {
|
||||
words.iter().map(|word| capitalize_first(word)).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_success() {
|
||||
assert_eq!(capitalize_first("hello"), "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty() {
|
||||
assert_eq!(capitalize_first(""), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterate_string_vec() {
|
||||
let words = vec!["hello", "world"];
|
||||
assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterate_into_string() {
|
||||
let words = vec!["hello", " ", "world"];
|
||||
assert_eq!(capitalize_words_string(&words), "Hello World");
|
||||
}
|
||||
}
|
||||
4
solutions/18_iterators/iterators3.rs
Normal file
4
solutions/18_iterators/iterators3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/18_iterators/iterators4.rs
Normal file
4
solutions/18_iterators/iterators4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
168
solutions/18_iterators/iterators5.rs
Normal file
168
solutions/18_iterators/iterators5.rs
Normal file
@@ -0,0 +1,168 @@
|
||||
// Let's define a simple model to track Rustlings' exercise progress. Progress
|
||||
// will be modelled using a hash map. The name of the exercise is the key and
|
||||
// the progress is the value. Two counting functions were created to count the
|
||||
// number of exercises with a given progress. Recreate this counting
|
||||
// functionality using iterators. Try to not use imperative loops (for/while).
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Progress {
|
||||
None,
|
||||
Some,
|
||||
Complete,
|
||||
}
|
||||
|
||||
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||
let mut count = 0;
|
||||
for val in map.values() {
|
||||
if *val == value {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
|
||||
// `map` is a hash map with `String` keys and `Progress` values.
|
||||
// map = { "variables1": Complete, "from_str": None, … }
|
||||
map.values().filter(|val| **val == value).count()
|
||||
}
|
||||
|
||||
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||
let mut count = 0;
|
||||
for map in collection {
|
||||
count += count_for(map, value);
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
|
||||
// `collection` is a slice of hash maps.
|
||||
// collection = [{ "variables1": Complete, "from_str": None, … },
|
||||
// { "variables2": Complete, … }, … ]
|
||||
collection
|
||||
.iter()
|
||||
.map(|map| count_iterator(map, value))
|
||||
.sum()
|
||||
}
|
||||
|
||||
// Equivalent to `count_collection_iterator` and `count_iterator`, iterating as
|
||||
// if the collection was a single container instead of a container of containers
|
||||
// (and more accurately, a single iterator instead of an iterator of iterators).
|
||||
fn count_collection_iterator_flat(
|
||||
collection: &[HashMap<String, Progress>],
|
||||
value: Progress,
|
||||
) -> usize {
|
||||
// `collection` is a slice of hash maps.
|
||||
// collection = [{ "variables1": Complete, "from_str": None, … },
|
||||
// { "variables2": Complete, … }, … ]
|
||||
collection
|
||||
.iter()
|
||||
.flat_map(HashMap::values) // or just `.flatten()` when wanting the default iterator (`HashMap::iter`)
|
||||
.filter(|val| **val == value)
|
||||
.count()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use Progress::*;
|
||||
|
||||
fn get_map() -> HashMap<String, Progress> {
|
||||
let mut map = HashMap::new();
|
||||
map.insert(String::from("variables1"), Complete);
|
||||
map.insert(String::from("functions1"), Complete);
|
||||
map.insert(String::from("hashmap1"), Complete);
|
||||
map.insert(String::from("arc1"), Some);
|
||||
map.insert(String::from("as_ref_mut"), None);
|
||||
map.insert(String::from("from_str"), None);
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
|
||||
let map = get_map();
|
||||
|
||||
let mut other = HashMap::new();
|
||||
other.insert(String::from("variables2"), Complete);
|
||||
other.insert(String::from("functions2"), Complete);
|
||||
other.insert(String::from("if1"), Complete);
|
||||
other.insert(String::from("from_into"), None);
|
||||
other.insert(String::from("try_from_into"), None);
|
||||
|
||||
vec![map, other]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_complete() {
|
||||
let map = get_map();
|
||||
assert_eq!(count_iterator(&map, Complete), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_some() {
|
||||
let map = get_map();
|
||||
assert_eq!(count_iterator(&map, Some), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_none() {
|
||||
let map = get_map();
|
||||
assert_eq!(count_iterator(&map, None), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_complete_equals_for() {
|
||||
let map = get_map();
|
||||
let progress_states = [Complete, Some, None];
|
||||
for progress_state in progress_states {
|
||||
assert_eq!(
|
||||
count_for(&map, progress_state),
|
||||
count_iterator(&map, progress_state),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_collection_complete() {
|
||||
let collection = get_vec_map();
|
||||
assert_eq!(count_collection_iterator(&collection, Complete), 6);
|
||||
assert_eq!(count_collection_iterator_flat(&collection, Complete), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_collection_some() {
|
||||
let collection = get_vec_map();
|
||||
assert_eq!(count_collection_iterator(&collection, Some), 1);
|
||||
assert_eq!(count_collection_iterator_flat(&collection, Some), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_collection_none() {
|
||||
let collection = get_vec_map();
|
||||
assert_eq!(count_collection_iterator(&collection, None), 4);
|
||||
assert_eq!(count_collection_iterator_flat(&collection, None), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_collection_equals_for() {
|
||||
let collection = get_vec_map();
|
||||
let progress_states = [Complete, Some, None];
|
||||
|
||||
for progress_state in progress_states {
|
||||
assert_eq!(
|
||||
count_collection_for(&collection, progress_state),
|
||||
count_collection_iterator(&collection, progress_state),
|
||||
);
|
||||
assert_eq!(
|
||||
count_collection_for(&collection, progress_state),
|
||||
count_collection_iterator_flat(&collection, progress_state),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
4
solutions/19_smart_pointers/arc1.rs
Normal file
4
solutions/19_smart_pointers/arc1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/19_smart_pointers/box1.rs
Normal file
4
solutions/19_smart_pointers/box1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/19_smart_pointers/cow1.rs
Normal file
4
solutions/19_smart_pointers/cow1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/19_smart_pointers/rc1.rs
Normal file
4
solutions/19_smart_pointers/rc1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/20_threads/threads1.rs
Normal file
4
solutions/20_threads/threads1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
41
solutions/20_threads/threads2.rs
Normal file
41
solutions/20_threads/threads2.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
// Building on the last exercise, we want all of the threads to complete their
|
||||
// work. But this time, the spawned threads need to be in charge of updating a
|
||||
// shared value: `JobStatus.jobs_done`
|
||||
|
||||
use std::{
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
struct JobStatus {
|
||||
jobs_done: u32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// `Arc` isn't enough if you want a **mutable** shared state.
|
||||
// We need to wrap the value with a `Mutex`.
|
||||
let status = Arc::new(Mutex::new(JobStatus { jobs_done: 0 }));
|
||||
// ^^^^^^^^^^^ ^
|
||||
|
||||
let mut handles = Vec::new();
|
||||
for _ in 0..10 {
|
||||
let status_shared = Arc::clone(&status);
|
||||
let handle = thread::spawn(move || {
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
|
||||
// Lock before you update a shared value.
|
||||
status_shared.lock().unwrap().jobs_done += 1;
|
||||
// ^^^^^^^^^^^^^^^^
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
// Waiting for all jobs to complete.
|
||||
for handle in handles {
|
||||
handle.join().unwrap();
|
||||
}
|
||||
|
||||
println!("Jobs done: {}", status.lock().unwrap().jobs_done);
|
||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
}
|
||||
4
solutions/20_threads/threads3.rs
Normal file
4
solutions/20_threads/threads3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/21_macros/macros1.rs
Normal file
4
solutions/21_macros/macros1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/21_macros/macros2.rs
Normal file
4
solutions/21_macros/macros2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/21_macros/macros3.rs
Normal file
4
solutions/21_macros/macros3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/21_macros/macros4.rs
Normal file
4
solutions/21_macros/macros4.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/22_clippy/clippy1.rs
Normal file
4
solutions/22_clippy/clippy1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/22_clippy/clippy2.rs
Normal file
4
solutions/22_clippy/clippy2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/22_clippy/clippy3.rs
Normal file
4
solutions/22_clippy/clippy3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/23_conversions/as_ref_mut.rs
Normal file
4
solutions/23_conversions/as_ref_mut.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/23_conversions/from_into.rs
Normal file
4
solutions/23_conversions/from_into.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/23_conversions/from_str.rs
Normal file
4
solutions/23_conversions/from_str.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/23_conversions/try_from_into.rs
Normal file
4
solutions/23_conversions/try_from_into.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/23_conversions/using_as.rs
Normal file
4
solutions/23_conversions/using_as.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
6
solutions/README.md
Normal file
6
solutions/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Official Rustlings solutions
|
||||
|
||||
Before you finish an exercise, its solution file will only contain an empty `main` function.
|
||||
The content of this file will be automatically replaced by the actual solution once you finish the exercise.
|
||||
|
||||
Note that these solutions are often only _one possibility_ to solve an exercise.
|
||||
4
solutions/quizzes/quiz1.rs
Normal file
4
solutions/quizzes/quiz1.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/quizzes/quiz2.rs
Normal file
4
solutions/quizzes/quiz2.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
4
solutions/quizzes/quiz3.rs
Normal file
4
solutions/quizzes/quiz3.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
// DON'T EDIT THIS SOLUTION FILE!
|
||||
// It will be automatically filled after you finish the exercise.
|
||||
}
|
||||
Reference in New Issue
Block a user