04 Rustlings quiz1 Solution
Now we have our first quiz building on what we've learned so far. Here is the code and instructions written in the comments.
quiz1.rs
// quiz1.rs
// This is a quiz for the following sections:
// - Variables
// - Functions
// - If
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given
// the quantity bought. No hints this time!
// I AM NOT DONE
// Put your function here!
// fn calculate_price_of_apples {
// Don't modify this function!
#[test]
fn verify_test() {
let price1 = calculate_price_of_apples(35);
let price2 = calculate_price_of_apples(40);
let price3 = calculate_price_of_apples(41);
let price4 = calculate_price_of_apples(65);
assert_eq!(70, price1);
assert_eq!(80, price2);
assert_eq!(41, price3);
assert_eq!(65, price4);
}
This is pretty straight forward, it's clear we have to write a little bit of code, let's recap what we know.
- Mary is buying apples.
- each apple is worth 2 rustbucks.
- If Mary purchases more than 40 apples, the apple price for each goes down to 1 rust buck for each apple.
- We need to create a function that calculates the correct price given the amount of apples purchased
quiz1.rs
errors
⚠️ Compiling of exercises/quiz1.rs failed! Please try again. Here's the output:
error[E0425]: cannot find function `calculate_price_of_apples` in this scope
--> exercises/quiz1.rs:21:18
|
21 | let price1 = calculate_price_of_apples(35);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `calculate_price_of_apples` in this scope
--> exercises/quiz1.rs:22:18
|
22 | let price2 = calculate_price_of_apples(40);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `calculate_price_of_apples` in this scope
--> exercises/quiz1.rs:23:18
|
23 | let price3 = calculate_price_of_apples(41);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `calculate_price_of_apples` in this scope
--> exercises/quiz1.rs:24:18
|
24 | let price4 = calculate_price_of_apples(65);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
error: aborting due to 4 previous errors
We are getting are getting 4 errors all related to the calculate_price_of_apples
function being out of scope. Well, that should be pretty obvious as to why, it's not there...or in this case commented out.
Working on a Solution
As noted in the errors, we need to implement the function fn_calculate_price_of_apples
so let's start there, by removing the comment forward slashes. We'll just add the code here without the additional comments.
// Put your function here!
fn calculate_price_of_apples {
// Don't modify this function!
#[test]
fn verify_test() {
let price1 = calculate_price_of_apples(35);
let price2 = calculate_price_of_apples(40);
let price3 = calculate_price_of_apples(41);
let price4 = calculate_price_of_apples(65);
assert_eq!(70, price1);
assert_eq!(80, price2);
assert_eq!(41, price3);
assert_eq!(65, price4);
If we think of what we've learned so far: variables
, if
statements and functions
we should be able to use these and apply them to our quiz here, let's start the function signature, we need to be explicit when defining our function signatures in Rust, so our function signature is missing a whole lot of information.
Completing our Function Signature
If we need any hints we can go back to our function
lesson and see how this current function looks incomplete, we should quickly notice that our current function signature looks incomplete compared to our previous exercises, so let's fix that, by defining a variable that gets passed through the function as an i32
, as well as adding a return value of i32
// how it's currently defined
fn calculate_price_of_apples {
}
This is how we're going to update the function signature:
- add the parameters of
apples:i32
- add a return value of
i32
so we can get that number from the function.
// passing through an `i32` that we define as `apples`
fn calculate_price_of_apples(apples:i32) -> i32 {
// also adding the ` -> i32` to show that we'll also return an `i32`
}
Adding Variables
Now let's look at variables we have to create inside of our function to complete the calculation of our price. I guess the most logical variables would be the price right, we have the regular price and the discount price.
fn calculate_price_of_apples(apples: i32) -> i32 {
let regular_price: i32 = 2; // defining a regular price
let discount_price: i32 = 1; // defining a dicount price
}
Easy enough right? We have defined a regular_price
and a discount_price
is based on the information we were given. Now let's go to the next step in our function defining our if
expression.
Controlling our Conditions with if
Let's quickly recap what we tools we have made so far:
- We've defined our function signature so it takes and returns and
i32
- We've created two different variables that define a
regular_price
or adiscounted_price
.
Now we need a way to let our function choose a branch of code depending the quantity of apples. The if
keyword let's us do this as we previously learned in lesson 3 so let's implement this within our function.
First let's think about this in plain English, remembering that Mary get's a price break after apple number 40, we can say that "if Mary buys 40 or less apples" it's regular price, "if Mary buys 41 or more apples" it's a discount price. Of course in code it's a little different and we can always reference the if
lesson for the syntax.
fn calculate_price_of_apples(apples: i32) -> i32 {
let regular_price: i32 = 2;
let discount_price: i32 = 1;
// defining if the quantity of `apples` purchased are 40 or less
if apples <= 40 {
// here's the simple math to calculate the result
apples * regular_price
// `else` let's us add the other part of the expression
} else {
// this multiples the number of apples which are above 40
apples * discount_price
}
}
And that's it, our code compiles and passes all tests, so we've finished our first quiz. This is what the final full code will look like, without all the comments for visual clarity:
fn calculate_price_of_apples(apples: i32) -> i32 {
let regular_price: i32 = 2;
let discount_price: i32 = 1;
if apples <= 40 {
apples * regular_price
} else {
apples * discount_price
}
}
// Don't modify this function!
#[test]
fn verify_test() {
let price1 = calculate_price_of_apples(35);
let price2 = calculate_price_of_apples(40);
let price3 = calculate_price_of_apples(41);
let price4 = calculate_price_of_apples(65);
assert_eq!(70, price1);
assert_eq!(80, price2);
assert_eq!(41, price3);
assert_eq!(65, price4);
}
Conclusion
In this blog post, we reviewed a Rust quiz about variables, functions, and if statements. We worked through the errors provided by the compiler and wrote a solution to calculate the price of apples based on the quantity bought. We first defined the function signature, which was incomplete, and added the appropriate information to pass and return an i32
. We then created variables for the regular and discounted prices of the apples. Finally, we used an if
expression to control the calculation of the price based on the quantity of apples bought, with a break for the discounted price starting at 40 apples.
By working through this quiz, we demonstrated how to apply the concepts of variables, functions, and if statements in Rust to solve a problem. We hope this blog post provides a clear and helpful review of these concepts and helps you better understand how to use them in your own Rust programming.