Skip to content

2022 Overhaul #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 34 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4e26f45
improve three exercises, add new modules exercise
CleanCut Oct 20, 2022
87b8116
use a workspace for the whole repository so we don't _have_ to open e…
CleanCut Oct 20, 2022
3f16561
overhaul the b_functions exercise entirely
CleanCut Nov 10, 2022
b13c600
add missing semicolons
CleanCut Nov 10, 2022
762f068
improve wording
CleanCut Nov 21, 2022
620b977
add readme
CleanCut Nov 21, 2022
b826f15
better wording
CleanCut Nov 21, 2022
617f17c
use constant names that alphabetize in the order I want
CleanCut Nov 21, 2022
d5812b9
better wording
CleanCut Nov 22, 2022
24fc259
improve the simple types exercise
CleanCut Dec 1, 2022
87884ee
polish exercise e
CleanCut Dec 18, 2022
260b366
rename exercise e to only control flow
CleanCut Jan 3, 2023
3f95325
refactor exercise e to use integers instead of strings, stop using ar…
CleanCut Jan 4, 2023
15769d5
fix links to updated exercises, improve exercise e wording
CleanCut Jan 4, 2023
d49f748
add new string exercise as f_strings
CleanCut Feb 11, 2023
ec01a15
move the ownership and references exercise from f to g
CleanCut Feb 14, 2023
69639a6
move the structs and traits exercise from g to h
CleanCut Feb 14, 2023
51772f5
split collections and enums exercise into two exercises
CleanCut Feb 14, 2023
ab92019
remove the workspace, since it causes an error when folks try to use …
CleanCut Feb 14, 2023
6f75c92
split the structs_traits exercise into two separate exercises
CleanCut Feb 18, 2023
7d76d09
improve struct exercise based on a practice run
CleanCut Feb 18, 2023
bb356a6
polish from another run-thru
CleanCut Mar 2, 2023
9f563c9
reorder a couple instructions
CleanCut Mar 31, 2023
3df0c36
a round of improvements to exercise i
CleanCut Apr 7, 2023
73db28f
another round of improvements to exercise i
CleanCut Apr 7, 2023
a90b0d1
third round
CleanCut Apr 7, 2023
6e9cb38
first draft of j_collections
CleanCut May 26, 2023
029d610
first pass of improvements
CleanCut May 26, 2023
cb89711
first draft of the new enums exercise
CleanCut Jun 20, 2023
e8f8ad1
remove the implemented code
CleanCut Jun 20, 2023
d1c9763
first round of polish
CleanCut Jun 20, 2023
c82582b
second round of polish
CleanCut Jun 20, 2023
eee4a23
fixes from first run-through
CleanCut Jun 21, 2023
fd10680
fix unnecessarily commented code
CleanCut Jun 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ For your convenience, here is a list of all the exercises, with links to view th

- [Exercise A - Variables & Scope](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/a_variables)
- [Exercise B - Functions](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/b_functions)
- [Exercise C - Simple Types](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/c_simple_types)
- [Exercise D - Control Flow & Strings](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/d_control_flow_strings)
- [Exercise E - Ownership & References](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/e_ownership_references)
- [Exercise F - Structs & Traits](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/f_structs_traits)
- [Exercise G - Collections & Enums](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/g_collections_enums)
- [Exercise H - Closures & Threads](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/h_closures_threads)
- [Exercise Z - Final Project](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/z_final_project)
- [Exercise C - Modules](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/c_modules)
- [Exercise D - Simple Types](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/d_simple_types)
- [Exercise E - Control Flow](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/e_control_flow)
- [Exercise F - Strings](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/f_strings)
- [Exercise G - Ownership & References](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/g_ownership_references)
- [Exercise ? - Structs & Traits](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/)
- [Exercise ? - Collections & Enums](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/)
- [Exercise Z - Image Manipulation](https://github.com/CleanCut/ultimate_rust_crash_course/tree/master/exercise/z_final_project)

# Projects

- [Invaders](https://github.com/CleanCut/invaders) - A terminal-based Space Invaders arcade game clone.
- TBD


[exercises]: https://github.com/CleanCut/ultimate_rust_crash_course#exercises
Expand Down
84 changes: 54 additions & 30 deletions exercise/b_functions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,63 @@
#![allow(unused_variables)]

fn main() {
let width = 4;
let height = 7;
let depth = 10;
// 1. Try running this code with `cargo run` and take a look at the error.
let number: f64 = 3.989; // don't change this line!

// 1. Try running the code and looking at the error. We would like to use the variable name
// `number` as an i32 (an integer), but it is already used as an f64 (a floating point number).
//
// See if you can fix the error. It is right around here, somewhere. If you succeed, then
// doing `cargo run` should succeed and print something out.
{
let area = area_of(width, height);
}
println!("Area is {}", area);

// 2. The area that was calculated is not correct! Go fix the area_of() function below, then run
// the code again and make sure it worked (you should get an area of 28).

// 3. Uncomment the line below. It doesn't work yet because the `volume` function doesn't exist.
// Create the `volume` function! It should:
// - Take three arguments of type i32
// - Multiply the three arguments together
// - Return the result (which should be 280 when you run the program).
// - Uncomment the commented-out code below
// - Complete the code to shadow the old `number` variable with a new `number` variable
// of the correct type.

// ... = convert_to_integer(number); // uncomment this line and finish shadowing `number`
inspect_integer(number); // don't change this line!

// 2. Uncomment and run the code below. Fix the scope problem so that the code compiles and runs
// producing the answer 42.

// {
// let answer = 42;
// }
// println!("The answer is {}", answer);

// 3. Create a function named `add` that adds two i32 values together and returns the result.
// Then uncomment the code below. You should get the output "4 + 42 = 46"
//
// If you get stuck, remember that this is *very* similar to what `area_of` does.
// Note: If you fixed the scope problem from #2 by moving the `println` up into the nested
// scope, then you will have to change the code above again so that `answer` is in this scope.

// let sum = ... // call your `add` function and pass it `number` and `answer` as arguments.
// println!("{} + {} = {}", number, answer, sum);

// 4. You can declare a variable without initializing it, but the compiler must be able to
// ensure that it will always be initialized before you can use it.
//
//println!("Volume is {}", volume(width, height, depth));
// Uncomment and run the code below to see the error. Fix the error by setting countdown to 0
// in the `else` branch of the `if` expression. Run the code. You should see a countdown of 10.

// let countdown: i32; // declares countdown, but doesn't initialize it
// if answer < 100 {
// countdown = 10;
// } else {
// println!("The answer is clearly wrong.");
// // set countdown to some value here
// }
// println!("The countdown begins at {}", countdown);
}

fn area_of(x: i32, y: i32) -> i32 {
// 2a. Fix this function to correctly compute the area of a rectangle given
// dimensions x and y by multiplying x and y and returning the result.
//
return 0;
// Challenge: It isn't idiomatic (the normal way a Rust programmer would do things) to use
// `return` on the last line of a function. Change the last line to be a
// "tail expression" that returns a value without using `return`.
// Hint: `cargo clippy` will warn you about this exact thing.
fn inspect_integer(x: i32) {
println!("The integer is {}", x);
}

// Challenge: A "tail expression" is when the last expression in a block does not end with a
// semicolon, making it the value of the block.
//
// - Refactor the body of this function to be a "tail expression" instead of a return statement.
// - Make the same change to the `add` function that you created
// - Run the code and make sure you get the same output as you did before
fn convert_to_integer(num: f64) -> i32 {
// For more information on using `as` to cast between numeric types, see:
// https://doc.rust-lang.org/reference/expressions/operator-expr.html#numeric-cast
return num.round() as i32;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[package]
name = "h_closures_threads"
name = "animal"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
crossbeam = "0.8.2"
File renamed without changes.
67 changes: 67 additions & 0 deletions exercise/c_modules/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 1. Organize code into a library without changing the output of the program.
//
// For each step of this exercise, you should be able to run the program before and after your
// change without affecting the output of the program.
//
// Move the constants below (FIRST, SECOND, and THIRD) into the library:
// - Create a `src/lib.rs` file
// - Move all of the constants into lib.rs
// - Make the constants public by adding the `pub` keyword in front of them
// - Add `use` statement(s) to main.rs to bring the constants into scope.
//
// Hint: the name of the library is defined in Cargo.toml

const FIRST: i32 = 1;
const SECOND: i32 = 2;
const THIRD: i32 = 3;

// 2. Create a library module named `sound` and move the animal functions into it.
//
// - In your lib.rs file add the line `pub mod sound;`
// - Create a `src/sound.rs` file for your module
// - Move the `dog`, `cat`, and `fox` functions into sound.rs
// - Make the functions public by adding the `pub` keyword in front of them
// - Add a `use` statement to bring the `sound` module into scope.
// - Change the function calls to access the functions through the `sound` module.
// For example: sound::dog()

fn dog() {
println!("Dog goes WOOF!");
}

fn cat() {
println!("Cat goes MEOW!");
}

fn fox() {
println!("What does the fox say???");
}

fn main() {
print!("Listening to animal {}: ", FIRST);
dog();

print!("Listening to animal {}: ", SECOND);
cat();

print!("Listening to animal {}: ", THIRD);
fox();
}

// Challenge 1
//
// - Move the `dog` and `cat` functions into a submodule `animal::sound::tame`
// - Move the `fox` function into a submodule `animal::sound::wild`
//
// Hint: You will need to create a subdirectory for the top-level `sound` modules' submodules to
// be placed in.

// Challenge 2
//
// Create an `animal::prelude` module which re-exports all of the constants and functions of the
// library. (A real library would only re-export the most commonly-used items in its prelude.)
//
// Change your `use` statement(s) in main.rs to just `use animal::prelude::*`
//
// Hint: You will need `pub use` to re-export an item, for more details see:
// https://doc.rust-lang.org/reference/items/use-declarations.html#use-visibility
83 changes: 0 additions & 83 deletions exercise/c_simple_types/src/main.rs

This file was deleted.

58 changes: 0 additions & 58 deletions exercise/d_control_flow_strings/src/main.rs

This file was deleted.

File renamed without changes.
Loading