user5402 argues to replace floating point with integer operations, but either way you should probably use unsigned integers as you don't need to support negative numbers. This allows you a bit more upper bound to your values as well.
user5402 argues to replace floating point with integer operations, but either way you should probably use unsigned integers as you don't need to support negative numbers. This allows you a bit more upper bound to your values as well.
Rust uses "Egyptian braces" in the majority of cases.
if condition { // block } else { // another block }
Use a space after a
:
in a type.fn foo(value: type)
Use spaces around operators like
==
,+
, and*
and around symbols like->
.i += 2;
Use a space after a
,
.println!("{}", x);
There's no need to use a
;
in one-line early return statements (guard clauses).if number == 2 { return true }
Use the
%
operator instead of calling therem
method:if number % 2 == 0 { return false }
Write the closure to
fold
inline, especially since you aren't able to give it a better name thanfold_op
(OP changed the code I am referring to; look in the revision history to see what I'm talking about).(1..to + 1).fold(1f64, |p, x| { // stuff })
Whenever possible, try to drive out mutability. I also dislike
return
statements inside of loop bodies (or really anywhere that aren't guard clauses). Try to use iterators instead.
Updates based on other answers
user5402 argues to replace floating point with integer operations, but either way you should probably use unsigned integers as you don't need to support negative numbers. This allows you a bit more upper bound to your values as well.
Rust uses "Egyptian braces".
if condition { // block }
Use a space after a
:
in a type.fn foo(value: type)
Use spaces around operators like
==
,+
, and*
and around symbols like->
.i += 2;
Use a space after a
,
.println!("{}", x);
There's no need to use a
;
in one-line early return statements (guard clauses).if number == 2 { return true }
Use the
%
operator instead of calling therem
method:if number % 2 == 0 { return false }
Write the closure to
fold
inline, especially since you aren't able to give it a better name thanfold_op
(OP changed the code I am referring to; look in the revision history to see what I'm talking about).(1..to + 1).fold(1f64, |p, x| { // stuff })
Whenever possible, try to drive out mutability. I also dislike
return
statements inside of loop bodies (or really anywhere that aren't guard clauses). Try to use iterators instead.
Rust uses "Egyptian braces" in the majority of cases.
if condition { // block } else { // another block }
Use a space after a
:
in a type.fn foo(value: type)
Use spaces around operators like
==
,+
, and*
and around symbols like->
.i += 2;
Use a space after a
,
.println!("{}", x);
There's no need to use a
;
in one-line early return statements (guard clauses).if number == 2 { return true }
Use the
%
operator instead of calling therem
method:if number % 2 == 0 { return false }
Write the closure to
fold
inline, especially since you aren't able to give it a better name thanfold_op
(OP changed the code I am referring to; look in the revision history to see what I'm talking about).(1..to + 1).fold(1f64, |p, x| { // stuff })
Whenever possible, try to drive out mutability. I also dislike
return
statements inside of loop bodies (or really anywhere that aren't guard clauses). Try to use iterators instead.
Updates based on other answers
user5402 argues to replace floating point with integer operations, but either way you should probably use unsigned integers as you don't need to support negative numbers. This allows you a bit more upper bound to your values as well.
Rust uses "Egyptian braces".
if condition { // block }
Use a space after a
:
in a type.fn foo(value: type)
Use spaces around operators like
==
,+
, and*
and around symbols like->
.i += 2;
Use a space after a
,
.println!("{}", x);
There's no need to use a
;
in one-line early return statements (guard clauses).if number == 2 { return true }
Use the
%
operator instead of calling therem
method:if number % 2 == 0 { return false }
Write the closure to
fold
inline, especially since you aren't able to give it a better name thanfold_op
(OP changed the code I am referring to; look in the revision history to see what I'm talking about).(1..to + 1).fold(1f64, |p, x| { // stuff })
Whenever possible, try to drive out mutability. I also dislike
return
statements inside of loop bodies (or really anywhere that aren't guard clauses). Try to use iterators instead.
I'm not well-versed on how to do the Euler problems in an efficient way, so hopefully someone else will chime in there.
Update after the original code changed
fn euler_problem5(to: i32) -> f64 {
(1..to + 1)
.filter(|&v| is_prime(v))
.fold(1f64, |p, x| p * near_pow(to as f64, x as f64))
}
Rust uses "Egyptian braces".
if condition { // block }
Use a space after a
:
in a type.fn foo(value: type)
Use spaces around operators like
==
,+
, and*
and around symbols like->
.i += 2;
Use a space after a
,
.println!("{}", x);
There's no need to use a
;
in one-line early return statements (guard clauses).if number == 2 { return true }
Use the
%
operator instead of calling therem
method:if number % 2 == 0 { return false }
Write the closure to
fold
inline, especially since you aren't able to give it a better name thanfold_op
.(1..to + 1).fold(1f64, |p, x| { // stuff })
Whenever possible, try to drive out mutability. I also dislike
return
statements inside of loop bodies (or really anywhere that aren't guard clauses). Try to use iterators instead.
I'm not well-versed on how to do the Euler problems in an efficient way, so hopefully someone else will chime in there.
Rust uses "Egyptian braces".
if condition { // block }
Use a space after a
:
in a type.fn foo(value: type)
Use spaces around operators like
==
,+
, and*
and around symbols like->
.i += 2;
Use a space after a
,
.println!("{}", x);
There's no need to use a
;
in one-line early return statements (guard clauses).if number == 2 { return true }
Use the
%
operator instead of calling therem
method:if number % 2 == 0 { return false }
Write the closure to
fold
inline, especially since you aren't able to give it a better name thanfold_op
(OP changed the code I am referring to; look in the revision history to see what I'm talking about).(1..to + 1).fold(1f64, |p, x| { // stuff })
Whenever possible, try to drive out mutability. I also dislike
return
statements inside of loop bodies (or really anywhere that aren't guard clauses). Try to use iterators instead.
I'm not well-versed on how to do the Euler problems in an efficient way, so hopefully someone else will chime in there.
Update after the original code changed
fn euler_problem5(to: i32) -> f64 {
(1..to + 1)
.filter(|&v| is_prime(v))
.fold(1f64, |p, x| p * near_pow(to as f64, x as f64))
}