I'm having trouble understanding (parameters? argument?) in functions and how I make them do stuff, you know, for the apple section in rustlings? Where you have to have the calculation change based on whether or not you have over 40 apples or not, you see I kinda didn't understand the 3.3 section on parameters and would like some help in my understanding. I used chat gpt via bing but that didn't help, how_do_i_do_math(with:this)? the bit in the paranthesis?
#Rust book Ch3, Rustlings quiz1.rs fn how_make(me:domath)?
23 messages · Page 1 of 1 (latest)
You need to make a function called calculate_price_of_apples, which calculates the total price for a number of apples. It takes 1 argument - the number of apples bought, and returns the total price.
So what part of function arguments is confusing you, defining them or using them?
Sorry, was afk. Ok, so I make the fn calculate_price_of_apples(apples:i32) -> i32 right, but because I misunderstood the question I write some crap about let rustbucks = 0 and apples = 0 and basically do "if apples > 40 {rustbucks = 1} else { rustbucks =2 }
rustbucks *= apples" it was at this point I looked closely and realized that the thing that test me is in the format of "assert_eq!(calculate_price_of_apples(35), 70);" and realized I was going on a wild goose chase up the wrong track, so I kept the apples as the name of the parameter but realized I don't know how you make this part"(35),70) do any calculation, I know that apples will equal the 35, but the 70? how do I bring it upstairs?
Here is what I got for reference:"fn calculate_price_of_apples(apples:i32) -> i32 {
apples* 5 "
and that's when I hit a wall.
So that assert_eq is just saying that the first value has to be equal to the second value, so calculate_price_of_apples(35) is equal to 70 which is double the value you give to the function calculate_price_of_apples
so would:"if apples > 40 {apples /= 2}" be going in the right direction?
My memory is crap so if it doesn't look right I'll look it up again, but let me know if I'm even close to where I'm trying to go.
/= is divide, so that will half the value of apples when you have more than 40. What you want to do is go the other direction, since you want to double when below rather than half when above 40
Yay, now I only have my poor math skills to overcome, it runs!:"---- tests::verify_test stdout ----
thread 'tests::verify_test' panicked at exercises/quizzes/quiz1.rs:31:9:
assertion left == right failed
left: 35
right: 70"
It seems you are not doubling, could you share the code you used for calculate_price_of_apples?
"fn calculate_price_of_apples(mut apples:i32) -> i32 {
if apples > 40{apples *= 2};
apples
}"
When are you trying to run the code apples *= 2?
When and where it told me to? I don't understand, can you please clarify?
after:" if apples > 40"? in the brackets {}? with the semicolon ;? and then I write apples on the next line to return the value for apples without the semicolon as it's a statement right?
So if it's after apples > 40 that means it'll only run when the number of apples is greater than (>) 40.
So you see, I've always failed math, and I've just realized my mistake... wait, hold on.
Okay, this is what worked: "fn calculate_price_of_apples(mut apples:i32) -> i32 {
if apples < 41 {apples *= 2};
apples
}"
Tried it with "if apples < 40" but that tripped up the 40 80 test.
That'll do it, if you want to make it clearer you can also use <= 40 which means less than or equal to