1. 13
    Rustlings test1: Writing a function that passes a set of tests by reading error messages
    2m 11s

Rustlings test1: Writing a function that passes a set of tests by reading error messages

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Chris Biscardi: [0:00] Now, we made our first field test. Up until this point, we've been going through exercises. This test file is a test for variables and functions as we've done the exercises already. In this case, all we have is a test. We can see the function verify test that is marked with a test.

[0:15] You can see price1 is = calculate_apple_price(35) and price2 = calculate_apple_price(65). We have an assert_eq! Macro that checks to see that 70 and price1 are the same, and 65 and price2 are the same.

[0:31] Our Rust error message tells us that the calculate_apple_price is not found in this scope which is true because we haven't put in our function in yet. We've included a function signature and we got quite a bit of output.

[0:43] For example, this function takes arguments, but 1 argument was supplied. That is the calculate_apple_price in the test is supplying an integer for us, but our function doesn't take one yet.

[0:52] If we fix that and use a type for it, we see that we get a new Rust error, "can't compare '{integer}' with '( )'," specifically is pointing to the assert_eq! Macro and saying that we can't compare 70 and unit. If we look back at our function, we can see that we aren't returning anything in the type signature which is why Rust is giving us this error.

[1:12] After fixing the type signature, we still have a mismatched type error. Rust expected i32 and found unit because we're implicitly returning unit because the body doesn't have any tail, which is a final expression or an explicit return. This is when we have to go back and read what the actual function is supposed to do.

[1:31] Thus, "Mary is buying apples. One apple usually costs 2 Rustbucks, but if you buy more than 40 at once, each apple only costs 1. Write a function that calculates the price of an order of apples given the order amount. No hints this time."

[1:43] Just from looking at the test and following the Rust error messages, we can't do a point where we have a function that takes a number and returns the number. Inside of the body of our function, we can just see if we're buying more than 40 apples.

[1:56] If we are buying more than 40, the apple price is going to be the same as the number of apples that we passed in because they cost one. Otherwise, they cost two. Finally, we can see that this implementation and the type signatures satisfy everything that Rust is looking for.

egghead
egghead
~ 25 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today