Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

user5402 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.

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.

added 327 characters in body
Source Link
Shepmaster
  • 8.8k
  • 27
  • 28
  1. Rust uses "Egyptian braces" in the majority of cases.

     if condition {
     // block
     } else {
     // another block
     }
    
  2. Use a space after a : in a type.

     fn foo(value: type)
    
  3. Use spaces around operators like ==, +, and * and around symbols like ->.

     i += 2;
    
  4. Use a space after a ,.

     println!("{}", x);
    
  5. There's no need to use a ; in one-line early return statements (guard clauses).

     if number == 2 { return true }
    
  6. Use the % operator instead of calling the rem method:

     if number % 2 == 0 { return false }
    
  7. Write the closure to fold inline, especially since you aren't able to give it a better name than fold_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
     })
    
  8. 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.

  1. Rust uses "Egyptian braces".

     if condition {
     // block
     }
    
  2. Use a space after a : in a type.

     fn foo(value: type)
    
  3. Use spaces around operators like ==, +, and * and around symbols like ->.

     i += 2;
    
  4. Use a space after a ,.

     println!("{}", x);
    
  5. There's no need to use a ; in one-line early return statements (guard clauses).

     if number == 2 { return true }
    
  6. Use the % operator instead of calling the rem method:

     if number % 2 == 0 { return false }
    
  7. Write the closure to fold inline, especially since you aren't able to give it a better name than fold_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
     })
    
  8. 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.

  1. Rust uses "Egyptian braces" in the majority of cases.

     if condition {
     // block
     } else {
     // another block
     }
    
  2. Use a space after a : in a type.

     fn foo(value: type)
    
  3. Use spaces around operators like ==, +, and * and around symbols like ->.

     i += 2;
    
  4. Use a space after a ,.

     println!("{}", x);
    
  5. There's no need to use a ; in one-line early return statements (guard clauses).

     if number == 2 { return true }
    
  6. Use the % operator instead of calling the rem method:

     if number % 2 == 0 { return false }
    
  7. Write the closure to fold inline, especially since you aren't able to give it a better name than fold_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
     })
    
  8. 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.

added 225 characters in body
Source Link
Shepmaster
  • 8.8k
  • 27
  • 28
  1. Rust uses "Egyptian braces".

     if condition {
     // block
     }
    
  2. Use a space after a : in a type.

     fn foo(value: type)
    
  3. Use spaces around operators like ==, +, and * and around symbols like ->.

     i += 2;
    
  4. Use a space after a ,.

     println!("{}", x);
    
  5. There's no need to use a ; in one-line early return statements (guard clauses).

     if number == 2 { return true }
    
  6. Use the % operator instead of calling the rem method:

     if number % 2 == 0 { return false }
    
  7. Write the closure to fold inline, especially since you aren't able to give it a better name than fold_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
     })
    
  8. 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))
}
  1. Rust uses "Egyptian braces".

     if condition {
     // block
     }
    
  2. Use a space after a : in a type.

     fn foo(value: type)
    
  3. Use spaces around operators like ==, +, and * and around symbols like ->.

     i += 2;
    
  4. Use a space after a ,.

     println!("{}", x);
    
  5. There's no need to use a ; in one-line early return statements (guard clauses).

     if number == 2 { return true }
    
  6. Use the % operator instead of calling the rem method:

     if number % 2 == 0 { return false }
    
  7. Write the closure to fold inline, especially since you aren't able to give it a better name than fold_op.

     (1..to + 1).fold(1f64, |p, x| {
     // stuff
     })
    
  8. 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.

  1. Rust uses "Egyptian braces".

     if condition {
     // block
     }
    
  2. Use a space after a : in a type.

     fn foo(value: type)
    
  3. Use spaces around operators like ==, +, and * and around symbols like ->.

     i += 2;
    
  4. Use a space after a ,.

     println!("{}", x);
    
  5. There's no need to use a ; in one-line early return statements (guard clauses).

     if number == 2 { return true }
    
  6. Use the % operator instead of calling the rem method:

     if number % 2 == 0 { return false }
    
  7. Write the closure to fold inline, especially since you aren't able to give it a better name than fold_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
     })
    
  8. 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))
}
Source Link
Shepmaster
  • 8.8k
  • 27
  • 28
Loading
lang-rust

AltStyle によって変換されたページ (->オリジナル) /