Recap - Product Array
Part of the Fundamentals section of Coddy's Rust journey — lesson 62 of 75.
Challenge
EasyWrite a function named prod which gets an array of numbers of size 8 as argument and returns the product of all the numbers in the list.
Reminder, product is the multiplication of all the numbers, for [1, 2, 3], return 6 = 1 * 2 * 3.
Try it yourself
use std::io;
use std::convert::TryInto;
fn prod(arr: &[i32]) -> i32 {
// Write your code below
}
fn main() {
let mut input_str_arr = String::new();
io::stdin().read_line(&mut input_str_arr).unwrap();
let numbers: [i32; 8] = input_str_arr.trim().split(',').map(|s| s.parse::<i32>().unwrap()).collect::<Vec<i32>>().try_into().unwrap();
let result = prod(&numbers);
println!("Product of array elements: {}", result);
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison13Arrays Continued
Mutable Reference ArraysArray MethodsRecap - Product ArrayRecap - Reversed ArrayBetter Print5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input