fn main() { let mut x = 3; println!("Number {}", x); x = 5; // don't change this line println!("Number {}", x); }
Ex 5
1 2 3 4 5 6
fn main() { let number = "T-H-R-E-E"; // don't change this line println!("Spell a Number : {}", number); let number = 3; // don't rename this variable println!("Number plus two is : {}", number + 2); }
fn main() { let answer = square(3); println!("The square of 3 is {}", answer); }
fn square(num: i32) -> i32 { num * num }
03_if(ch3.5)
Ex 1
一个简单的比大小
1 2 3 4 5 6 7 8 9 10 11 12
pub fn bigger(a: i32, b: i32) -> i32 { // Complete this function to return the bigger number! // If both numbers are equal, any of them can be returned. // Do not use: // - another function call // - additional variables if a > b { return a } else { b } }
// 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(num: i32) -> i32 { if num > 40 { num } else { num * 2 } }
// 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);