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
.zelsazgh
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum:i32 = 0; if start > end { 0 }else{ for i in start..=end { let even = i % 2; if even == 0{ sum += i; } } sum }}
VladyslavY
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut summ:i32 = 0; for n in start..=end { print!("{}", n.to_string()); if n % 2 == 0 {summ += n;}; }; return summ; }
digitalresistor
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } let mut res: i32 = 0; for i in start..=end { if i % 2 == 0 { res += i; } } res}
jose-bernardo
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut ans: i32 = 0; for n in start..end + 1 { if n % 2 == 0 { ans += n; } } ans}
jose-bernardo
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut ans: i32 = 0; for n in start..end + 1 { if n % 2 == 0 { ans += n; } } ans}
Maki-SIO
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } let mut res: i32 = 0; for n in start..end+1 { if n % 2 == 0 { res += n; } } res}
crrodger
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } let mut sum = 0; for i in start..=end { if i % 2 == 0 { sum += i; } } return sum}
radloffl
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } let mut sum = 0; for i in start ..= end { let remainder = i % 2; if remainder == 0 { sum = sum + i; } } return sum;}
just-mephit
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|n| n % 2 == 0).sum()}
carlos-quantexai
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = vec![]; for i in start..=end{ if i % 2 == 0{ result.push(i) } } result.iter().sum()}
oTTeuMsTudio
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).filter(|x| x%2==0).sum()}
dslex35
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|x| x%2 ==0).sum()}
infamous-711
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0 } (start..=end) .into_iter() .filter(|x| x % 2 == 0) .sum()}
ThatphumCpre
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|x| x%2==0).sum()}
jhq223
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let result: i32 = (start..=end).filter(|x| x % 2 == 0).sum(); result}
francisco-cmyk
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0 } let mut total = 0; for i in start..=end { if i % 2 == 0 { total += i } } return total}
xbarnett
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|n| n % 2 == 0).sum()}
mk-comm
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 { if n % 2 == 0 {sum = sum + n; } }; sum}
nordicmachine
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = 0; for x in start ..= end { if x % 2 == 0 { result = result + x; } } return result}
MosesMp3
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end{ return 0; } let mut total = 0; for i in start..=end{ if i%2==0{ total+=i; } } total}
majesticalcreature
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... if start > end { return 0; } let mut sum: i32 = 0; for i in start..end+1 { if i % 2 == 0 { sum += i; } } sum}
josschne
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for n in start..end+1 { if n % 2 == 0 {sum+= n} } sum}
sander-b-postnl
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|x| x % 2 == 0).sum()}
matei
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } let mut sum = 0; for i in start..end+1 { if i % 2 == 0 { sum += i; } } return sum;}
st01011
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0 } else { let mut sum = 0; for x in start..end+1 { if x % 2 == 0{ sum = sum + x; } } return sum }}
hendrikdevloed
use std::cmp;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}
komarspn
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).filter(|v| v % 2 == 0).sum()}
amassare
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).filter(|x|x%2==0).sum()}
edoardo-morosanu
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut num: i32 = 0; for n in start..=end { if n % 2 == 0 { num += n; } } num}
zavakid
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).into_iter().filter(|x| x % 2 == 0).sum()}
IdoPort
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... (start..=end).into_iter().filter(|i| i % 2 == 0).sum()}
victorchukwuemeka
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 } } sum }
Johnchoi913
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... //O(1) solution if start >= end { return 0; } let even_start = { if start % 2 == 0 { start / 2 } else { (start + 1) / 2 } }; let even_end = { if end % 2 == 0 { end / 2 } else { (end - 1) / 2 } }; if start == end { return start; } if (even_start >= 0 && even_end >= 0){ return (even_end - even_start + 1) * (even_start + even_end ); } else{ return (even_end + even_start + 1) * (even_start + even_end ) * -1; }}
qapd01
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}
JijoBose
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } (start..end+1).into_iter().filter(|i| i % 2 == 0).sum()}
Parallaxes
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; } } sum}
Rational1ty
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } let mut sum = 0; for n in start..=end { if n % 2 != 0 { continue; } sum += n; } return sum;}
brum-b0
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... println!("start: {}, end: {}", start, end); if start > end { 0 } else { let mut sum :i32 = 0; for n in start..end+1 { println!("n: {}", n); if n % 2 == 0 { sum += n; println!("sum: {}", sum); } } sum } }
tiagombsilva
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum: i32 = 0; for i in start..end + 1 { if i % 2 == 0 { println!("{}", i); sum += i; } } return sum;}
i5-650
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start > end { return 0; } (start..end+1).into_iter().filter(|i| i % 2 == 0).sum()}
retinotopic
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum: i32 = 0; if start > end { return sum; } for i in start..=end { if i % 2 == 0 { sum+=i; } } sum}
Ustin
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for i in start..end+1 { if i%2 == 0 {sum += i;} }; sum}
ogaca42
pub fn sum_of_evens(start: i32, end: i32) -> i32 { let mut sum = 0; for s in start..end+1 { if s % 2 == 0 { sum += s } } sum}
otherJL0
pub fn sum_of_evens(start: i32, end: i32) -> i32 { if start >= end { 0 } else { (start..=end).filter(|num| num % 2 == 0).sum() }}
pbjarterot
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for number in start..end+1 { if number % 2 == 0 { sum += number } } sum}
CianciuStyles
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}
ayushrawat10
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut result = 0; for i in start..=end { result += match i%2 { 0 => i, _ => 0, } } result}
agegorin
pub fn sum_of_evens(start: i32, end: i32) -> i32 { (start..=end).filter(|x| x % 2 == 0).sum()}
damascussteel21
pub fn sum_of_evens(start: i32, end: i32) -> i32 { // Your code here... let mut sum = 0; for num in start..=end { if num % 2 == 0 { sum += num; } } sum}
agegorin
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}