Skip to content

Commit 4da8f6d

Browse files
committed
// using_as.rs
// // Type casting in Rust is done via the usage of the `as` operator. Please note // that the `as` operator is not only used when type casting. It also helps with // renaming imports. // // The goal is to make sure that the division does not fail to compile and // returns the proper type. // // Execute `rustlings hint using_as` or use the `hint` watch subcommand for a // hint. // I AM NOT DONE fn average(values: &[f64]) -> f64 { let total = values.iter().sum::<f64>(); total / values.len() as f64 // 使用 as 将 usize 转换为 f64 } fn main() { let values = [3.5, 0.3, 13.0, 11.7]; println!("{}", average(&values)); } #[cfg(test)] mod tests { use super::*; #[test] fn returns_proper_type_and_value() { assert_eq!(average(&[3.5, 0.3, 13.0, 11.7]), 7.125); } }
1 parent 14bfd2c commit 4da8f6d

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

exercises/conversions/using_as.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
fn average(values: &[f64]) -> f64 {
1616
let total = values.iter().sum::<f64>();
17-
total / values.len()
17+
total / values.len() as f64 // 使用 as 将 usize 转换为 f64
1818
}
1919

2020
fn main() {

0 commit comments

Comments
 (0)