rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

quiz1.rs (954B)


      1 // This is a quiz for the following sections:
      2 // - Variables
      3 // - Functions
      4 // - If
      5 //
      6 // Mary is buying apples. The price of an apple is calculated as follows:
      7 // - An apple costs 2 rustbucks.
      8 // - However, if Mary buys more than 40 apples, the price of each apple in the
      9 // entire order is reduced to only 1 rustbuck!
     10 
     11 // TODO: Write a function that calculates the price of an order of apples given
     12 // the quantity bought.
     13 // fn calculate_price_of_apples(???) -> ??? { ??? }
     14 fn calculate_price_of_apples(n: i32) -> i32 {
     15     if n <= 40 { n * 2 } else { n }
     16 }
     17 
     18 fn main() {
     19     // You can optionally experiment here.
     20 }
     21 
     22 // Don't change the tests!
     23 #[cfg(test)]
     24 mod tests {
     25     use super::*;
     26 
     27     #[test]
     28     fn verify_test() {
     29         assert_eq!(calculate_price_of_apples(35), 70);
     30         assert_eq!(calculate_price_of_apples(40), 80);
     31         assert_eq!(calculate_price_of_apples(41), 41);
     32         assert_eq!(calculate_price_of_apples(65), 65);
     33     }
     34 }