-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharithmetic-operators.rs
32 lines (26 loc) · 1.11 KB
/
arithmetic-operators.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
println!("{}", 10 + 20); // 30
println!("{}", 20 - 10); // 10
println!("{}", 10 * 20); // 200
println!("{}", 20 / 10); // 2
// power (type is required)
println!("{}", i32::pow(10, 2));
// 100
// floating-point conversion
println!("{}", 10.0 + ((10 + 20) as f64));
// 40
// common division error
println!("{}", 30 / 20); // 1 (quotient)
println!("{}", 30.0 / 20.0); // 1.5
// modulo, remainder
println!("{}", 12.5 % 10.0); // 2.5
println!("{}", 10.0 % 20.0); // 10
// built-ins (type is required)
println!("{}", i32::abs(-20)); // 20
println!("{}", i32::min(2, 5)); // 2
println!("{}", i32::max(2, 5)); // 5
// rounding values (type is required)
println!("{}", f64::round(2.945)); // 3
println!("{}", f64::trunc(2.945)); // 2
println!("{}", f64::floor(2.945)); // 2
}