1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-08-08 22:36:55 +02:00

Merge branch 'main' into rc-exercise

This commit is contained in:
liv
2022-08-16 09:37:34 +02:00
committed by GitHub
20 changed files with 329 additions and 42 deletions

View File

@ -5,20 +5,22 @@
| variables | §3.1 |
| functions | §3.3 |
| if | §3.5 |
| move_semantics | §4.1, §4.2 |
| primitive_types | §3.2, §4.3 |
| vecs | §8.1 |
| move_semantics | §4.1, §4.2 |
| structs | §5.1, §5.3 |
| enums | §6, §18.3 |
| modules | §7 |
| collections | §8.1, §8.3 |
| strings | §8.2 |
| modules | §7 |
| hashmaps | §8.3 |
| options | §10.1 |
| error_handling | §9 |
| generics | §10 |
| option | §10.1 |
| traits | §10.2 |
| tests | §11.1 |
| lifetimes | §10.3 |
| standard_library_types | §13.2, §15.1, §16.3 |
| threads | §16.1 |
| threads | §16.1, §16.2, §16.3 |
| macros | §19.6 |
| clippy | n/a |
| conversions | n/a |

View File

@ -4,6 +4,8 @@
// This exercise uses some concepts that we won't get to until later in the course, like `Box` and the
// `From` trait. It's not important to understand them in detail right now, but you can read ahead if you like.
// For now, think of the `Box<dyn ...>` type as an "I want anything that does ???" type, which, given
// Rust's usual standards for runtime safety, should strike you as somewhat lenient!
// In short, this particular use case for boxes is for when you want to own a value and you care only that it is a
// type which implements a particular trait. To do so, The Box is declared as of type Box<dyn Trait> where Trait is the trait

View File

@ -7,4 +7,4 @@ macros. Instead, we'll show you how to use and create them.
## Further information
- [Macros](https://doc.rust-lang.org/book/ch19-06-macros.html)
- [The Little Book of Rust Macros](https://danielkeep.github.io/tlborm/book/index.html)
- [The Little Book of Rust Macros](https://veykril.github.io/tlborm/)

View File

@ -3,17 +3,13 @@
// I AM NOT DONE
// you can modify anything EXCEPT for this function's signature
fn print_number(maybe_number: Option<u16>) {
println!("printing: {}", maybe_number.unwrap());
}
// This function returns how much icecream there is left in the fridge.
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
// all, so there'll be no more left :(
// TODO: Return an Option!
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// We use the 24-hour system here, so 10PM is a value of 22
// The Option output should gracefully handle cases where time_of_day > 24.
???
}
@ -23,15 +19,17 @@ mod tests {
#[test]
fn check_icecream() {
assert_eq!(maybe_icecream(10), Some(5));
assert_eq!(maybe_icecream(23), None);
assert_eq!(maybe_icecream(22), None);
assert_eq!(maybe_icecream(9), Some(5));
assert_eq!(maybe_icecream(10), Some(0));
assert_eq!(maybe_icecream(23), Some(0));
assert_eq!(maybe_icecream(22), Some(0));
assert_eq!(maybe_icecream(25), None);
}
#[test]
fn raw_value() {
// TODO: Fix this test. How do you get at the value contained in the Option?
let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5);
assert_eq!(icecreams, 0);
}
}

View File

@ -42,7 +42,7 @@ mod my_module {
#[cfg(test)]
mod tests {
// TODO: What to we have to import to have `transformer` in scope?
// TODO: What do we have to import to have `transformer` in scope?
use ???;
use super::Command;

View File

@ -10,7 +10,7 @@
// 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 `unimplemented!()`
// Step 2: create both empty and non-empty cons lists by replacing `todo!()`
//
// Note: the tests should not be changed
//
@ -33,11 +33,11 @@ fn main() {
}
pub fn create_empty_list() -> List {
unimplemented!()
todo!()
}
pub fn create_non_empty_list() -> List {
unimplemented!()
todo!()
}
#[cfg(test)]

View File

@ -0,0 +1,48 @@
// 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.
// I AM NOT DONE
use std::borrow::Cow;
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
for i in 0..input.len() {
let v = input[i];
if v < 0 {
// Clones into a vector if not already owned.
input.to_mut()[i] = -v;
}
}
input
}
fn main() {
// No clone occurs because `input` doesn't need to be mutated.
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
Cow::Borrowed(_) => println!("I borrowed the slice!"),
_ => panic!("expected borrowed value"),
}
// Clone occurs because `input` needs to be mutated.
let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
Cow::Owned(_) => println!("I modified the slice and now own it!"),
_ => panic!("expected owned value"),
}
// No clone occurs because `input` is already owned.
let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => println!("I own this slice!"),
_ => panic!("expected borrowed value"),
}
}

View File

@ -4,7 +4,7 @@
// I AM NOT DONE
fn trim_me(input: &str) -> String {
// TODO: Remove whitespace from the end of a string!
// TODO: Remove whitespace from both ends of a string!
???
}

View File

@ -1,7 +1,7 @@
// traits4.rs
//
// Your task is to replace the '??' sections so the code compiles.
// Don't change any line other than 21.
// Don't change any line other than the marked one.
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@ -19,6 +19,7 @@ struct OtherSoftware {}
impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
// YOU MAY ONLY CHANGE THE NEXT LINE
fn compare_license_types(software: ??, software_two: ??) -> bool {
software.licensing_info() == software_two.licensing_info()
}

View File

@ -1,7 +1,7 @@
// traits5.rs
//
// Your task is to replace the '??' sections so the code compiles.
// Don't change any line other than 27.
// Don't change any line other than the marked one.
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@ -18,15 +18,20 @@ pub trait OtherTrait {
}
}
struct SomeStruct {
name: String,
}
struct SomeStruct {}
struct OtherStruct {}
impl SomeTrait for SomeStruct {}
impl OtherTrait for SomeStruct {}
impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}
// YOU MAY ONLY CHANGE THE NEXT LINE
fn some_func(item: ??) -> bool {
item.some_function() && item.other_function()
}
fn main() {}
fn main() {
some_func(SomeStruct {});
some_func(OtherStruct {});
}