In this challenge, you will practice using the for
loop in Rust to iterate over a range of numbers. The goal is to sum all the even numbers within a given range.
The for
loop in Rust is a powerful construct that allows you to iterate over collections, ranges, and iterators. It is commonly used for tasks such as summing numbers, collecting items, and more. By utilizing the for
loop, you can write concise and readable code to perform repetitive tasks.
Your task is to implement a function sum_of_evens
that takes two arguments, start
and end
, both of which are i32
. The function should return the sum of all even numbers within the range [start, end]
inclusive. If start
is greater than end
, the function should return 0
.
for
loop to iterate over the range from start
to end
.start
is greater than end
, return 0
.jimlawton
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } else { let range = std::ops::Range { start, end: end + 1 }; range.filter(|a| if a % 2 == 0 {true} else {false}).sum() }}
woke-developer
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut total: i32 = 0; //if start < end { for num in start..=end { if num % 2 == 0 { total += num; } } //} total}
mbergkvist
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for n in start..=end { if n % 2 == 0 { sum += n } } sum}
5822791760
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; }; let mut sum = 0; for n in start..end + 1 { if n % 2 == 0 { sum += n; }; }; println!("{}", start); println!("{}", end); println!("{}", sum); return sum;}
leenalmajz
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum: i32 = 0; if start > end { return sum } else { for i in start..end+1 { if is_even(i) { sum += i } } return sum } }pub fn is_even(n: i32) -> bool { if n % 2 == 0 { return true } else { return false }}
facat
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start>end{ return 0; } let mut sum:i32=0; for v in start..(end+1){ if v%2==0{ sum+=v; } } return sum;}
DistributedDoge
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end{ return(0) } let mut acc = 0; for x in start..end + 1{ if x % 2 == 0 { acc = acc + x; } } acc}
xenonminer
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for n in start..=end { if (n % 2 == 0) { sum += n; } } return sum;}
XtebanUy
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result : i32 = 0; for i in start..=end { if i % 2 == 0 { result += i; } } result}
mehdihmr
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = 0; for i in start..=end{ if i % 2 == 0{ result += i; } } return result;}
ethel-dev
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for i in start..end + 1 { if i % 2 == 0 { sum += i; } } sum}
DivineGod
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).filter(|n| n%2==0).sum()}
Algorab
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..end + 1).into_iter().filter(|x| x % 2 == 0).sum()}
Hannnsen
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for i in start..end + 1 { if i % 2 == 0 { sum = sum + i; } } sum}
sroas
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0 } (start..=end).filter(|it| it % 2 == 0).sum()}
itaygenkin
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for i in start..=end { if i % 2 == 0 { sum += i; } } return sum;}
felipebalbi
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).filter(|n| n & 1 == 0).sum()}
danielmpetrov
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|x| x % 2 == 0).sum()}
yoakemae
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum: i32 = 0; for n in start..=end { if n % 2 == 0 { sum += n; } } return sum;}
qiyuan711
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end) .enumerate() .filter(|&(_, x)| x % 2 == 0) .map(|(_, x)| x) .sum()}
jw
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0 } let mut total = 0; for n in start..=end { if n % 2 == 0 { total += n } } total}
nickythorne
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } (start..=end).filter(|n| n % 2 == 0).sum()}
chuyuanlinzi
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start < end { (start..=end).filter(|i| i % 2 == 0).sum() } else { 0 }}
DV-13
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start <= end { (start..end + 1).filter(|i| i % 2 == 0).sum() } else { 0i32 }}
stdstring
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for number in start..=end { if (number%2) == 0 { sum += number; } } sum}
tinthid
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|n| n % 2 == 0).sum()}
martin-unit
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = 0; for i in start..=end { if i%2==0 { result = result + i; } } result}
RonaldoArayadev
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } let mut sum:i32 = 0; for n in start..=end { if n % 2 == 0 { sum += n; } } sum}
dalprahcd
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for i in start..=end { if i % 2 == 0 { sum += i; } } sum}
3ok
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0 } else { let mut sum = 0; for i in start..end + 1 { if i % 2 == 0 { sum += i; } } return sum }}
Thymelizabeth
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start + if start % 2 == 0 { 0 } else { 1 }..=end).step_by(2).sum()}
Thymelizabeth
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut result = 0; for i in start..=end { if i % 2 == 0 { result += i; } } result}
devarajang
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum:i32 = 0; for i in start..=end { if i%2 == 0 { sum = sum + i; } } return sum;}
jeypiti
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).into_iter().filter(|x| x % 2 == 0).sum()}
konishu
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0 } else { let mut sum_ = 0; for temp in start..=end { if temp % 2 == 0 { sum_ += temp } } return sum_ }}
oDqnger
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0 } else { let mut sum = 0; for i in (start..end+1) { if i % 2 == 0 { sum += i } } sum }}
nichideropa
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { () } let mut sum: i32 = 0; for num in start..=end { if num % 2 == 0 { sum += num; } } sum}
aleksey-vareto
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for number in start..=end { if number %2 == 0 { sum += number } } sum}
aleksey-vareto
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for number in start..end + 1 { if number %2 == 0 { sum += number } } sum}
akshayabhattarai
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } let mut sum: i32 = 0; for a in start..=end { if a%2 == 0{ sum +=a; } } sum}
0xsmarter
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; // we first define the sum here for x in start..=end { // we start the loop from start to end if x % 2 == 0 { sum += x; } } sum // we are the total sum here }
madeinheaven91
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for i in start..=end{ if i % 2 == 0 { sum += i; } } sum}
aidan1magee
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut summed: i32 = 0; for n in start..=end{ if n%2 == 0{ summed += n; } } summed}
ankeetparikh
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for x in start..=end { if x % 2 == 0 { sum += x; } } sum}
maxvi
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } let mut sum = 0; for i in start..=end { if i % 2 == 0 { sum += i; } } sum}
hackermajorsreekumar
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum_evens = 0; for n in start..=end{ if n % 2 == 0 { sum_evens += n; } } sum_evens}
koslowskio
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for n in start..=end { if n % 2 == 0 { sum = sum + n; } } return sum}
rafaellucasprogm
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = 0; for n in start..=end { if n % 2 == 0 { result += n } } result}
rafaellucasprogm
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = 0; for n in start..=end { if n % 2 == 0 { result += n } } result}
cloki0610
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).filter(|i| i % 2 == 0).sum()}