Skip to content

Fix example code for unreachable! #19360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2014
Merged
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
9 changes: 4 additions & 5 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,11 @@ macro_rules! debug_assert_eq(
/// Iterators:
///
/// ```rust
/// fn divide_by_three(x: i32) -> i32 { // one of the poorest implementations of x/3
/// for i in std::iter::count(0_i32, 1) {
/// if i < 0 { panic!("i32 overflow"); }
/// if x < 3*i { return i; }
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
/// for i in std::iter::count(0_u32, 1) {
/// if 3*i < i { panic!("u32 overflow"); }
/// if x < 3*i { return i-1; }
Copy link
Contributor

Choose a reason for hiding this comment

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

<= 3*i should be sufficient (and more clear), no? Using checked_mul would also make more sense to me than the explicit panic guard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, with <= 3*i, this gives:

0/3=0
1/3=1
2/3=1
3/3=1
4/3=2
5/3=2
6/3=2

Copy link
Contributor

Choose a reason for hiding this comment

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

Derp, yes.

/// }
///
/// unreachable!();
/// }
/// ```
Expand Down