From 2f810a4da67233716ad93e00afff6e8b260f4807 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 17 Apr 2024 23:34:27 +0200 Subject: Clean up and unify exercises --- exercises/19_smart_pointers/arc1.rs | 4 -- exercises/19_smart_pointers/box1.rs | 4 -- exercises/19_smart_pointers/cow1.rs | 4 -- exercises/19_smart_pointers/rc1.rs | 109 +++++++++++++++++++----------------- 4 files changed, 57 insertions(+), 64 deletions(-) (limited to 'exercises/19_smart_pointers') diff --git a/exercises/19_smart_pointers/arc1.rs b/exercises/19_smart_pointers/arc1.rs index 0647eea..7b31fa8 100644 --- a/exercises/19_smart_pointers/arc1.rs +++ b/exercises/19_smart_pointers/arc1.rs @@ -1,5 +1,3 @@ -// arc1.rs -// // In this exercise, we are given a Vec of u32 called "numbers" with values // ranging from 0 to 99 -- [ 0, 1, 2, ..., 98, 99 ] We would like to use this // set of numbers within 8 different threads simultaneously. Each thread is @@ -18,8 +16,6 @@ // first TODO comment is, and create an initial binding for `child_numbers` // where the second TODO comment is. Try not to create any copies of the // `numbers` Vec! -// -// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. #![forbid(unused_imports)] // Do not change this, (or the next) line. use std::sync::Arc; diff --git a/exercises/19_smart_pointers/box1.rs b/exercises/19_smart_pointers/box1.rs index 2abc024..226a117 100644 --- a/exercises/19_smart_pointers/box1.rs +++ b/exercises/19_smart_pointers/box1.rs @@ -1,5 +1,3 @@ -// box1.rs -// // At compile time, Rust needs to know how much space a type takes up. This // becomes problematic for recursive types, where a value can have as part of // itself another value of the same type. To get around the issue, we can use a @@ -15,8 +13,6 @@ // Step 2: create both empty and non-empty cons lists by replacing `todo!()` // // Note: the tests should not be changed -// -// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint. #[derive(PartialEq, Debug)] pub enum List { diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index 51e5fdb..754c0ba 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_smart_pointers/cow1.rs @@ -1,5 +1,3 @@ -// cow1.rs -// // This exercise explores the Cow, or Clone-On-Write type. Cow is a // clone-on-write smart pointer. It can enclose and provide immutable access to // borrowed data, and clone the data lazily when mutation or ownership is @@ -9,8 +7,6 @@ // This exercise is meant to show you what to expect when passing data to Cow. // Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the // TODO markers. -// -// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. use std::borrow::Cow; diff --git a/exercises/19_smart_pointers/rc1.rs b/exercises/19_smart_pointers/rc1.rs index e96e625..19de3db 100644 --- a/exercises/19_smart_pointers/rc1.rs +++ b/exercises/19_smart_pointers/rc1.rs @@ -1,5 +1,3 @@ -// rc1.rs -// // In this exercise, we want to express the concept of multiple owners via the // Rc type. This is a model of our solar system - there is a Sun type and // multiple Planets. The Planets take ownership of the sun, indicating that they @@ -7,8 +5,6 @@ // // Make this code compile by using the proper Rc primitives to express that the // sun has multiple owners. -// -// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint. use std::rc::Rc; @@ -33,71 +29,80 @@ impl Planet { } } -#[test] fn main() { - let sun = Rc::new(Sun {}); - println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference + // You can optionally experiment here. +} - let mercury = Planet::Mercury(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 2 references - mercury.details(); +#[cfg(test)] +mod tests { + use super::*; - let venus = Planet::Venus(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 3 references - venus.details(); + #[test] + fn rc1() { + let sun = Rc::new(Sun {}); + println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference - let earth = Planet::Earth(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 4 references - earth.details(); + let mercury = Planet::Mercury(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 2 references + mercury.details(); - let mars = Planet::Mars(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 5 references - mars.details(); + let venus = Planet::Venus(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 3 references + venus.details(); - let jupiter = Planet::Jupiter(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 6 references - jupiter.details(); + let earth = Planet::Earth(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 4 references + earth.details(); - // TODO - let saturn = Planet::Saturn(Rc::new(Sun {})); - println!("reference count = {}", Rc::strong_count(&sun)); // 7 references - saturn.details(); + let mars = Planet::Mars(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 5 references + mars.details(); - // TODO - let uranus = Planet::Uranus(Rc::new(Sun {})); - println!("reference count = {}", Rc::strong_count(&sun)); // 8 references - uranus.details(); + let jupiter = Planet::Jupiter(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 6 references + jupiter.details(); - // TODO - let neptune = Planet::Neptune(Rc::new(Sun {})); - println!("reference count = {}", Rc::strong_count(&sun)); // 9 references - neptune.details(); + // TODO + let saturn = Planet::Saturn(Rc::new(Sun {})); + println!("reference count = {}", Rc::strong_count(&sun)); // 7 references + saturn.details(); - assert_eq!(Rc::strong_count(&sun), 9); + // TODO + let uranus = Planet::Uranus(Rc::new(Sun {})); + println!("reference count = {}", Rc::strong_count(&sun)); // 8 references + uranus.details(); - drop(neptune); - println!("reference count = {}", Rc::strong_count(&sun)); // 8 references + // TODO + let neptune = Planet::Neptune(Rc::new(Sun {})); + println!("reference count = {}", Rc::strong_count(&sun)); // 9 references + neptune.details(); - drop(uranus); - println!("reference count = {}", Rc::strong_count(&sun)); // 7 references + assert_eq!(Rc::strong_count(&sun), 9); - drop(saturn); - println!("reference count = {}", Rc::strong_count(&sun)); // 6 references + drop(neptune); + println!("reference count = {}", Rc::strong_count(&sun)); // 8 references - drop(jupiter); - println!("reference count = {}", Rc::strong_count(&sun)); // 5 references + drop(uranus); + println!("reference count = {}", Rc::strong_count(&sun)); // 7 references - drop(mars); - println!("reference count = {}", Rc::strong_count(&sun)); // 4 references + drop(saturn); + println!("reference count = {}", Rc::strong_count(&sun)); // 6 references - // TODO - println!("reference count = {}", Rc::strong_count(&sun)); // 3 references + drop(jupiter); + println!("reference count = {}", Rc::strong_count(&sun)); // 5 references - // TODO - println!("reference count = {}", Rc::strong_count(&sun)); // 2 references + drop(mars); + println!("reference count = {}", Rc::strong_count(&sun)); // 4 references - // TODO - println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference + // TODO + println!("reference count = {}", Rc::strong_count(&sun)); // 3 references - assert_eq!(Rc::strong_count(&sun), 1); + // TODO + println!("reference count = {}", Rc::strong_count(&sun)); // 2 references + + // TODO + println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference + + assert_eq!(Rc::strong_count(&sun), 1); + } } -- cgit v1.2.3