Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions exercises/nth-prime/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ fn is_prime(n: u32) -> bool {
return true;
}

pub fn nth(n: u32) -> Result<u32, ()> {
pub fn nth(n: u32) -> Option<u32> {
match n {
0 => Err(()),
1 => Ok(2),
0 => None,
1 => Some(2),
_ => {
let mut count: u32 = 1;
let mut candidate: u32 = 1;
Expand All @@ -22,7 +22,7 @@ pub fn nth(n: u32) -> Result<u32, ()> {
count += 1;
}
}
Ok(candidate)
Some(candidate)
}
}
}
3 changes: 3 additions & 0 deletions exercises/nth-prime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn nth(n: u32) -> Option<u32> {
unimplemented!("What is the {}th prime number?", n)
}
10 changes: 5 additions & 5 deletions exercises/nth-prime/tests/nth-prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ extern crate nth_prime as np;

#[test]
fn test_first_prime() {
assert_eq!(np::nth(1), Ok(2));
assert_eq!(np::nth(1), Some(2));
}

#[test]
#[ignore]
fn test_second_prime() {
assert_eq!(np::nth(2), Ok(3));
assert_eq!(np::nth(2), Some(3));
}

#[test]
#[ignore]
fn test_sixth_prime() {
assert_eq!(np::nth(6), Ok(13));
assert_eq!(np::nth(6), Some(13));
}

#[test]
#[ignore]
fn test_big_prime() {
assert_eq!(np::nth(10001), Ok(104743));
assert_eq!(np::nth(10001), Some(104743));
}

#[test]
#[ignore]
fn test_zeroth_prime() {
assert!(np::nth(0).is_err());
assert_eq!(np::nth(0), None);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this from assert! to assert_eq! was a good call, the error message goes from:

---- test_zeroth_prime stdout ----
	thread 'test_zeroth_prime' panicked at 'assertion failed: np::nth(0).is_none()', tests/nth-prime.rs:28:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

to the much clearer

---- test_zeroth_prime stdout ----
	thread 'test_zeroth_prime' panicked at 'assertion failed: `(left == right)`
  left: `Some(1)`,
 right: `None`', tests/nth-prime.rs:28:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

You can see the full output of both options here.

}