Skip to content

Commit c4328aa

Browse files
authored
Merge pull request #437 from cbzehner/nth-prime
nth-prime: Provide a function template
2 parents 7050814 + 2e50c9b commit c4328aa

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

exercises/nth-prime/example.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ fn is_prime(n: u32) -> bool {
99
return true;
1010
}
1111

12-
pub fn nth(n: u32) -> Result<u32, ()> {
12+
pub fn nth(n: u32) -> Option<u32> {
1313
match n {
14-
0 => Err(()),
15-
1 => Ok(2),
14+
0 => None,
15+
1 => Some(2),
1616
_ => {
1717
let mut count: u32 = 1;
1818
let mut candidate: u32 = 1;
@@ -22,7 +22,7 @@ pub fn nth(n: u32) -> Result<u32, ()> {
2222
count += 1;
2323
}
2424
}
25-
Ok(candidate)
25+
Some(candidate)
2626
}
2727
}
2828
}

exercises/nth-prime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn nth(n: u32) -> Option<u32> {
2+
unimplemented!("What is the {}th prime number?", n)
3+
}

exercises/nth-prime/tests/nth-prime.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ extern crate nth_prime as np;
22

33
#[test]
44
fn test_first_prime() {
5-
assert_eq!(np::nth(1), Ok(2));
5+
assert_eq!(np::nth(1), Some(2));
66
}
77

88
#[test]
99
#[ignore]
1010
fn test_second_prime() {
11-
assert_eq!(np::nth(2), Ok(3));
11+
assert_eq!(np::nth(2), Some(3));
1212
}
1313

1414
#[test]
1515
#[ignore]
1616
fn test_sixth_prime() {
17-
assert_eq!(np::nth(6), Ok(13));
17+
assert_eq!(np::nth(6), Some(13));
1818
}
1919

2020
#[test]
2121
#[ignore]
2222
fn test_big_prime() {
23-
assert_eq!(np::nth(10001), Ok(104743));
23+
assert_eq!(np::nth(10001), Some(104743));
2424
}
2525

2626
#[test]
2727
#[ignore]
2828
fn test_zeroth_prime() {
29-
assert!(np::nth(0).is_err());
29+
assert_eq!(np::nth(0), None);
3030
}

0 commit comments

Comments
 (0)