using_as.rs (592B)
1 // Type casting in Rust is done via the usage of the `as` operator. 2 // Note that the `as` operator is not only used when type casting. It also helps 3 // with renaming imports. 4 5 fn average(values: &[f64]) -> f64 { 6 let total = values.iter().sum::<f64>(); 7 // TODO: Make a conversion before dividing. 8 total / values.len() 9 } 10 11 fn main() { 12 let values = [3.5, 0.3, 13.0, 11.7]; 13 println!("{}", average(&values)); 14 } 15 16 #[cfg(test)] 17 mod tests { 18 use super::*; 19 20 #[test] 21 fn returns_proper_type_and_value() { 22 assert_eq!(average(&[3.5, 0.3, 13.0, 11.7]), 7.125); 23 } 24 }