Compare commits
10 Commits
2129073c03
...
7194a5d29e
| Author | SHA1 | Date | |
|---|---|---|---|
|
7194a5d29e
|
|||
|
|
5e852ca5d5 | ||
|
|
0b362a2a7b | ||
|
|
1b1ced37a4 | ||
|
|
de71e01a95 | ||
|
|
c100b83038 | ||
|
|
5c0dfcda33 | ||
|
|
ea93715738 | ||
|
|
036e7b7b50 | ||
|
|
a47afbbb6e |
@@ -1,2 +1,3 @@
|
||||
[workspace]
|
||||
members = [ "example/*", "exercise/*" ]
|
||||
resolver = "2"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Ultimate Rust 2: Intermediate Concepts
|
||||
|
||||
This is the companion repository for the `Ultimate Rust 2: Intermediate Concepts` (the followup to the popular [Ultimate Rust Crash Course]). _UR2IC_ will be published independently online in the second half of 2021 and is also presented live as part of some O'Reilly virtual events such as [Rust in 3 Weeks], or taught in-person for corporate training. You will get the most out of this training experience by doing the [exercises] in this repository and watching (or attending) the instructor-led training.
|
||||
This is the companion repository for the `Ultimate Rust 2: Intermediate Concepts` (the followup to the popular [Ultimate Rust Crash Course]). You will get the most out of this training experience by doing the [exercises] in this repository and watching (or attending) the instructor-led training.
|
||||
|
||||
In other words, this repository is for you hands-on-learners!
|
||||
|
||||
@@ -22,7 +22,7 @@ The exercises are separate Rust projects inside the `exercises/` subdirectory.
|
||||
- Open up the `src/main.rs` file.
|
||||
- Follow the numbered exercise instructions in the code comments.
|
||||
|
||||
If you encounter any problems with the exercises, please feel free to use the online course communication tools to contact me, or [open an discussion]. Either way. 😄
|
||||
If you encounter any problems with the exercises, please feel free to use the online course communication tools to contact me, or [open a discussion]. Either way. 😄
|
||||
|
||||
For your convenience, here is a list of all the exercises, with links to view the code on GitHub.
|
||||
|
||||
@@ -55,7 +55,7 @@ If you like the work I do, please consider sponsoring me [on GitHub] or [on Patr
|
||||
|
||||
[exercises]: https://github.com/CleanCut/ultimate_rust2#exercises
|
||||
[`example/`]: https://github.com/CleanCut/ultimate_rust2/blob/main/example
|
||||
[open an discussion]: https://github.com/CleanCut/ultimate_rust2/discussions/new
|
||||
[open a discussion]: https://github.com/CleanCut/ultimate_rust2/discussions/new
|
||||
[Ultimate Rust Crash Course]: https://agileperception.com/ultimate_rust_crash_course
|
||||
[Rust in 3 Weeks]: https://agileperception.com
|
||||
[Ultimate Rust 2: Intermediate Concepts]: https://github.com/CleanCut/ultimate_rust2
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// START IN lib.rs!
|
||||
|
||||
use aquarium::Dolphin;
|
||||
// Silence some warnings so they don't distract from the exercise.
|
||||
#[allow(clippy::vec_init_then_push)]
|
||||
|
||||
// (You already did #1 in lib.rs, right?)
|
||||
//
|
||||
|
||||
@@ -8,16 +8,28 @@
|
||||
|
||||
// Challenge: Clippy doesn't find *everything*. What else would you change to make this code better?
|
||||
|
||||
const pi:f32=3.14159265358979323846;
|
||||
fn count_to_5()->i32{let mut foo =0;loop{if foo>pi as i32{if foo > 5{break;}}foo=foo+1;}return 5;}
|
||||
fn main() {
|
||||
println!("I can count to {}", count_to_5());
|
||||
use std::f32::consts::PI;
|
||||
|
||||
fn count_to_5() -> i32 {
|
||||
let mut count = 0;
|
||||
loop {
|
||||
if count > PI as i32 && count >= 5 {
|
||||
break;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
fn main() {
|
||||
println!("I can count to {}", count_to_5());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_counting() {
|
||||
assert_eq!(count_to_5() == 5, true);
|
||||
}
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_counting() {
|
||||
assert_eq!(count_to_5() == 5, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,9 @@ impl Frog {
|
||||
}
|
||||
}
|
||||
pub fn sleep(&mut self) {
|
||||
if !self.sleeping {
|
||||
if self.sleeping {
|
||||
// 5. Use error!() to log a (non-fatal) error stating that the Frog is already asleep
|
||||
} else {
|
||||
self.sleeping = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ fn main() {
|
||||
skippy.hop();
|
||||
skippy.hop();
|
||||
skippy.sleep();
|
||||
skippy.sleep();
|
||||
|
||||
// Challenge: Go back to lib.rs and set the `target: ` argument for each logging call to be the
|
||||
// path to the function. For example, `Frog::new`
|
||||
|
||||
@@ -18,7 +18,7 @@ fn expensive_sum(v: Vec<i32>) -> i32 {
|
||||
fn main() {
|
||||
let my_vector = vec![2, 5, 1, 0, 4, 3];
|
||||
|
||||
// 1. Spawn a child thread and have it call `expensive_sum(my_vector)`. Store the returned
|
||||
// 1. Spawn a child thread and have it call `expensive_sum(my_vector)`. Store the returned
|
||||
// join handle in a variable called `handle`. Once you've done this you should be able to run
|
||||
// the code and see the output from the child thread's expensive sum in the middle of the main
|
||||
// thread's processing of letters.
|
||||
@@ -79,8 +79,8 @@ fn main() {
|
||||
});
|
||||
|
||||
// Using a Receiver channel as an iterator is a convenient way to get values until the channel
|
||||
// gets closed. A Receiver channel is automatically closed once all Sender channels have been
|
||||
// closed. Both our threads automatically close their Sender channels when they exit and the
|
||||
// gets closed. A Receiver channel is automatically closed once all Sender channels have been
|
||||
// closed. Both our threads automatically close their Sender channels when they exit and the
|
||||
// destructors for the channels get automatically called.
|
||||
for msg in rx {
|
||||
println!("Main thread: Received {}", msg);
|
||||
@@ -90,10 +90,10 @@ fn main() {
|
||||
// - Use the thread handles to join both threads without getting any compiler warnings.
|
||||
*/
|
||||
|
||||
// Challenge: Make two child threads and give them each a receiving end to a channel. From the
|
||||
// Challenge: Make two child threads and give them each a receiving end to a channel. From the
|
||||
// main thread loop through several values and print each out and then send it to the channel.
|
||||
// On the child threads print out the values you receive. Close the sending side in the main
|
||||
// thread by calling `drop(tx)` (assuming you named your sender channel variable `tx`). Join
|
||||
// thread by calling `drop(tx)` (assuming you named your sender channel variable `tx`). Join
|
||||
// the child threads.
|
||||
println!("Main thread: Exiting.")
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ fn main() {
|
||||
|
||||
// println!("The default Party is\n{:#?}", Party::default());
|
||||
|
||||
// 4. You prefer Maple Bacon cake. Use "struct update syntax" to create a Dessert with `cake`
|
||||
// 4. You prefer Maple Bacon cake. Use "struct update syntax" to create a Party with `cake`
|
||||
// set to `Cake::MapleBacon`, but the rest of the values are default.
|
||||
//
|
||||
// Hint: The trick to struct update syntax is specifying the value(s) you want to customize
|
||||
|
||||
Reference in New Issue
Block a user