RUST 函数定义中,参数必须显性声明类型
rust
// The type of function arguments must be annotated.
// Added the type annotation `u64`.
fn call_me(num: u64) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
fn main() {
call_me(3);
}
函数的最后一个语句的变量就是返回值。
函数返回值类型必须显式声明
注意:作为返回值的最后一个语句末尾不能带分号!
rust
fn is_even(num: i64) -> bool {
num % 2 == 0
}
// The return type must always be annotated.
fn sale_price(price: i64) -> i64 {
if is_even(price) {
price - 10
} else {
price - 3
}
}
fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}