Menu
Coddy logo textTech

Recap - Product Array

Part of the Fundamentals section of Coddy's Rust journey — lesson 62 of 75.

challenge icon

Challenge

Easy

Write 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