mirror of
https://github.com/notohh/rustlings.git
synced 2025-10-08 19:48:39 -04:00
docs: cleanup the explanation paragraphs at the start of each exercise.
This commit is contained in:
parent
30291a3c25
commit
7eef5d15ee
95 changed files with 577 additions and 337 deletions
|
@ -1,21 +1,24 @@
|
|||
// 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 going to get the sum of every eighth value, with an offset.
|
||||
//
|
||||
// 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
|
||||
// going to get the sum of every eighth value, with an offset.
|
||||
//
|
||||
// The first thread (offset 0), will sum 0, 8, 16, ...
|
||||
// The second thread (offset 1), will sum 1, 9, 17, ...
|
||||
// The third thread (offset 2), will sum 2, 10, 18, ...
|
||||
// ...
|
||||
// The eighth thread (offset 7), will sum 7, 15, 23, ...
|
||||
|
||||
//
|
||||
// Because we are using threads, our values need to be thread-safe. Therefore,
|
||||
// we are using Arc. We need to make a change in each of the two TODOs.
|
||||
|
||||
|
||||
//
|
||||
// Make this code compile by filling in a value for `shared_numbers` where the
|
||||
// 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!
|
||||
// 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.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
// 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 `Box` - a smart pointer used to store data on the heap,
|
||||
// which also allows us to wrap a recursive type.
|
||||
// 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
|
||||
// `Box` - a smart pointer used to store data on the heap, which also allows us
|
||||
// to wrap a recursive type.
|
||||
//
|
||||
// The recursive type we're implementing in this exercise is the `cons list` - a data structure
|
||||
// frequently found in functional programming languages. Each item in a cons list contains two
|
||||
// elements: the value of the current item and the next item. The last item is a value called `Nil`.
|
||||
// The recursive type we're implementing in this exercise is the `cons list` - a
|
||||
// data structure frequently found in functional programming languages. Each
|
||||
// item in a cons list contains two elements: the value of the current item and
|
||||
// the next item. The last item is a value called `Nil`.
|
||||
//
|
||||
// Step 1: use a `Box` in the enum definition to make the code compile
|
||||
// Step 2: create both empty and non-empty cons lists by replacing `todo!()`
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
// 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 required.
|
||||
// The type is designed to work with general borrowed data via the Borrow trait.
|
||||
//
|
||||
// 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
|
||||
// required. The type is designed to work with general borrowed data via the
|
||||
// Borrow trait.
|
||||
//
|
||||
// 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.
|
||||
// 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.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
@ -50,10 +54,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn owned_no_mutation() -> Result<(), &'static str> {
|
||||
// We can also pass `slice` without `&` so Cow owns it directly.
|
||||
// In this case no mutation occurs and thus also no clone,
|
||||
// but the result is still owned because it was never borrowed
|
||||
// or mutated.
|
||||
// We can also pass `slice` without `&` so Cow owns it directly. In this
|
||||
// case no mutation occurs and thus also no clone, but the result is
|
||||
// still owned because it was never borrowed or mutated.
|
||||
let slice = vec![0, 1, 2];
|
||||
let mut input = Cow::from(slice);
|
||||
match abs_all(&mut input) {
|
||||
|
@ -63,9 +66,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn owned_mutation() -> Result<(), &'static str> {
|
||||
// Of course this is also the case if a mutation does occur.
|
||||
// In this case the call to `to_mut()` returns a reference to
|
||||
// the same data as before.
|
||||
// Of course this is also the case if a mutation does occur. In this
|
||||
// case the call to `to_mut()` returns a reference to the same data as
|
||||
// before.
|
||||
let slice = vec![-1, 0, 1];
|
||||
let mut input = Cow::from(slice);
|
||||
match abs_all(&mut input) {
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
// rc1.rs
|
||||
// In this exercise, we want to express the concept of multiple owners via the Rc<T> 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 revolve around the sun.
|
||||
|
||||
// Make this code compile by using the proper Rc primitives to express that the sun has multiple owners.
|
||||
//
|
||||
// In this exercise, we want to express the concept of multiple owners via the
|
||||
// Rc<T> 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
|
||||
// revolve around the sun.
|
||||
//
|
||||
// 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.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue