Table of contents
Rust Programming Tips(8)
1. Rounding function floor()
The floor function rounds down a floating-point number
Sample code:
fn main() {
let x: f32 = 3.23;
let s = x.floor();
println!("{}", s);
}
output:
3
2. Rounding function ceil()
The ceil function rounds up a floating-point number
Sample code:
fn main() {
let x: f32 = 3.23;
let s = x.ceil();
println!("{}", s);
}
output:
4
3. Rounding function round()
The round function rounds floating-point numbers to integers
Sample code:
fn main() {
let x: f32 = 3.23;
let s = x.round();
println!("{}", s);
let x: f32 = 3.63;
let s = x.round();
println!("{}", s);
}
output:
3
4
4. Keep decimal places
Use the above three rounding functions to achieve the goal:
fn main() {
let mut x: f32 = 3.1415926;
let s = format!("{:.4}", x);
println!("{}\n", s);
let s = (x * 10000.0).round() / 10000.0;
println!("{}", s);
let s = (x * 10000.0).ceil() / 10000.0;
println!("{}", s);
let s = (x * 10000.0).floor() / 10000.0;
println!("{}\n", s);
x = 3.1415333;
let s = (x * 10000.0).round() / 10000.0;
println!("{}", s);
let s = (x * 10000.0).ceil() / 10000.0;
println!("{}", s);
let s = (x * 10000.0).floor() / 10000.0;
println!("{}", s);
}
5. String to Integer
unwrap()
Used to get the Ok value from the Result object. If the Result object is Err, panic! is called directly to raise a runtime error.
Sample code:
fn main() {
let s = "123";
let i = s.parse::<i32>().unwrap();
println!("{:?}", i);
let s = "12x3";
let i = s.parse::<i32>();
println!("{:?}", i);
}
output:
123
Err(ParseIntError { kind: InvalidDigit })
unwrap_or()
Used to get the value from the Result object, if the Result object is Err, then return a default value.
Sample code:
fn main() {
let s = "123";
let i: i32 = s.parse().unwrap_or(0);
println!("{:?}", i);
let s = "12x3";
let i = s.parse().unwrap_or(0);
println!("{:?}", i);
}
output:
123
0
Matching pattern: let ... match
fn main() {
let s = "123";
let i = match s.parse::<i32>() {
Ok(i) => i,
Err(_e) => -1,
};
println!("{:?}", i);
let s = "12x3";
let i = match s.parse::<i32>() {
Ok(i) => i,
Err(_e) => -1,
};
println!("{:?}", i);
}
output:
123
-1
Attachment: match syntax
is a pattern matching tool for multi-branch comparisons based on characteristics of values. Its basic syntax is as follows:
match value {
pattern1 => result1,
pattern2 => result2,
...
_ => resultN,
}
The value here is the value to be matched, which can be compared with each pattern. If the value matches a certain pattern, the corresponding result is executed. If none of the patterns match, the last _ => resultN will be executed.
pattern can be any value or pattern that might be matched. For example, you can use variables to capture values in patterns, or use range patterns to match specific values.
let number = 3;
match number {
1 => println!("Number is 1"),
2 => println!("Number is 2"),
3 => println!("Number is 3"),
_ => println!("Number is not 1, 2, or 3"),
}
In this example, the value of number is 3, so it matches the third pattern and prints "Number is 3".
Alternatively, you can use range patterns, for example:
let number = 5;
match number {
1..=5 => println!("Number is between 1 and 5"),
_ => println!("Number is not between 1 and 5"),
}
in this example , the value of number is 5, so it matches the first pattern and prints "Number is between 1 and 5".
Related Reading:
Rust programming tips selection (1)_Hann Yang's Blog-CSDN Blog
Rust programming tips selection (2)_Hann Yang's blog - CSDN blog
Rust Programming Tips Excerpt (3)_Hann Yang's Blog - CSDN Blog
Rust Programming Tips Excerpt (4)_Hann Yang's Blog - CSDN Blog
Rust Programming Tips Excerpt (5)_Hann Yang's Blog - CSDN Blog
Rust Programming Tips Excerpt (6)_Hann Yang's Blog - CSDN Blog
Rust Programming Tips Excerpt (7)_Hann Yang's Blog - CSDN Blog