Browse Source

feat(prime_checker): add new rust exercise

DEV-4017-prototypes-exercise-1-animals
nprimo 2 years ago committed by Niccolò Primo
parent
commit
92d91c30f8
  1. 67
      subjects/prime_checker/README.md

67
subjects/prime_checker/README.md

@ -0,0 +1,67 @@
## prime_checker
### Instructions
Create a **function** `prime_checker` that takes an `u32` and check if it is a prime number.
The result will be `None` if the argument is less or equal one otherwise, it will return the result with the prime number inside if the number is prime, or it will return an `enum` `PrimeErr` if the `u32` is not prime.
The `enum` `PrimeErr` will be `Even` if the number is even or `Divider(u32)` where the `u32` is the smaller divider of the number if it is not prime.
### Expected Function and structure
```rust
#[derive(PartialEq, Eq, Debug)]
pub enum PrimeErr {
Even,
Divider(u32),
}
pub fn prime_checker(nb: u32) -> Option<Result<u32, PrimeErr>> {}
```
### Usage
Here is a program to test your function:
```rust
use prime_checker::*;
fn main() {
println!(
"Is {} prime? {}",
2,
match prime_checker(2) {
Some(res) => {
match res {
Ok(_) => "Yes, it is!",
Err(_) => "No, it is not!",
}
}
None => "Less than 2",
}
);
println!(
"Is {} prime? {}",
14,
match prime_checker(14) {
Some(res) => {
match res {
Ok(_) => "Yes, it is!",
Err(_) => "No, it is not!",
}
}
None => "Less than 2",
}
);
}
```
And its output:
```console
$ cargo run
Is 2 prime? Yes, it is!
Is 14 prime? No, it is not!
$
```
Loading…
Cancel
Save