Ad

Welcome to kaju bakery,

Kaju bakery deals with large number of orders of cake. So to handle the cake demand we need a function that takes in the number of servings of cake required and price of cake in dollar and returns the total price for that cake. We also impose tax of 1$ so keep that in mind too

Have the function named deliver and return the total price customer has to pay in the counter.

also check if the number of servings <= 0 then just return zero.

Code
Diff
  • fn price(servings: i32, price: i32) -> i32 {
        match servings > 0 {
            true => servings * price + 1,
            false => 0
        }
    }
    
    • fn price(servings: u32, price: u32) -> u32 {
    • let mut cost = servings * price;
    • if servings > 0 {
    • cost += 1
    • fn price(servings: i32, price: i32) -> i32 {
    • match servings > 0 {
    • true => servings * price + 1,
    • false => 0
    • }
    • cost
    • }
Test Cases
Diff
  • #[test]
    fn test_case() {
        assert_eq!(price(2, 3), 7);
        assert_eq!(price(4, 5), 21);
        assert_eq!(price(0, 10), 0);
        assert_eq!(price(-1, 5), 0)
    }
    • #[test]
    • fn test_case() {
    • assert_eq!(price(2, 3), 7);
    • assert_eq!(price(4, 5), 21);
    • assert_eq!(price(0, 10), 0);
    • assert_eq!(price(-1, 5), 0)
    • }