Arrays are a fundamental data structure in Rust that allow you to store a fixed-size collection of elements of the same type. A common operation is to calculate the sum of all elements in an array.
In this challenge, you will implement a function to calculate the sum of elements in an array of integers i32
.
You need to implement the function sum_array(arr: &[i32]) -> i32
that takes a slice of integers and returns the sum of all elements.
sum_array
function should return the sum of all elements in the array..iter()
method to iterate over the elements of the array..sum()
method to calculate the sum of the elements.qiyuan711
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
tuusberg
pub fn sum_array(arr: &[i32]) -> i32 { let mut total: i32 = 0; for element in arr { total += element; } return total;}
apst
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
jw
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
nickythorne
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
alexmerot
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here return arr.iter().sum();}
terminox
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
ryzen-xp
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
Derteq
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
chuyuanlinzi
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
DV-13
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
tinthid
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
yoakemae
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here return arr.into_iter().sum();}
martin-unit
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
Neil-Rayu
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum: i32 = 0; for i in arr.iter() { sum += i; } return sum;}
RonaldoArayadev
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
3ok
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
leenalmajz
pub fn sum_array(arr: &[i32]) -> i32 { let mut sum = 0; for a in arr { sum = sum + a } return sum}
dpey
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here return arr.iter().sum();}
Thymelizabeth
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
devarajang
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum : i32 = 0; for i in 0..arr.len() { //println!("{0}", arr[i]) sum = sum + arr[i]; } return sum;}
amilcarrey
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum = 0; sum = arr.iter().sum(); return sum;}
danredtmf
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
SCBenson
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let sum = arr.iter().sum(); return sum;}
jeypiti
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
konishu
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let sum = arr.into_iter().sum(); return sum;}
nichideropa
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
aleksey-vareto
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum() // let mut sum = 0; // for element in arr { // sum += element; // } // sum}
akshayabhattarai
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum = 0; for a in arr{ sum += a; } sum}
aleksey-vareto
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
akshayabhattarai
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum = 0; for a in arr{ sum += a; } sum}
M4dPac
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
M4dPac
pub fn sum_array(arr: &[i32]) -> i32 { let mut res = 0; for el in arr { res += el; } res}
oDqnger
pub fn sum_array(arr: &[i32]) -> i32 { let mut sum = 0; for i in arr { sum += i; } sum}
kk6
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().fold(0, |sum, i| sum + i)}
otakumesi
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here return arr.iter().sum()}
matsumura-y1
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum = 0; for x in arr { sum += x; } return sum;}
matsuyama-k1
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum:i32 = 0; for val in arr.iter() { sum = sum + val; } return sum;}
sy34
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here return arr.iter().sum();}
matsuby
pub fn sum_array(arr: &[i32]) -> i32 { return arr.iter().sum();}
ankeetparikh
pub fn sum_array(arr: &[i32]) -> i32 { let mut sum = 0; for x in arr { sum += x; } sum}
0xsmarter
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
aidan1magee
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
aidan1magee
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
maxvi
pub fn sum_array(arr: &[i32]) -> i32 { arr.iter().sum()}
hackermajorsreekumar
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here let mut sum = 0; for i in arr{ sum+=i; } sum}
johandroid
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}
hfm
pub fn sum_array(arr: &[i32]) -> i32 { return arr.iter().sum();}
P0TER19
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here return arr.iter().sum();}
WalquerX
pub fn sum_array(arr: &[i32]) -> i32 { // TODO: Implement the function here arr.iter().sum()}