Skip to content

Commit 8999a79

Browse files
committed
rollup merge of rust-lang#19316: steveklabnik/gh18876
Fixes rust-lang#18876
2 parents 52ca952 + 4ce3ba4 commit 8999a79

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/libstd/macros.rs

+33-17
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,42 @@ macro_rules! debug_assert_eq(
186186
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); })
187187
)
188188

189-
/// A utility macro for indicating unreachable code. It will panic if
190-
/// executed. This is occasionally useful to put after loops that never
191-
/// terminate normally, but instead directly return from a function.
189+
/// A utility macro for indicating unreachable code.
192190
///
193-
/// # Example
191+
/// This is useful any time that the compiler can't determine that some code is unreachable. For
192+
/// example:
193+
///
194+
/// * Match arms with guard conditions.
195+
/// * Loops that dynamically terminate.
196+
/// * Iterators that dynamically terminate.
197+
///
198+
/// # Panics
199+
///
200+
/// This will always panic.
201+
///
202+
/// # Examples
203+
///
204+
/// Match arms:
194205
///
195-
/// ```{.rust}
196-
/// struct Item { weight: uint }
197-
///
198-
/// fn choose_weighted_item(v: &[Item]) -> Item {
199-
/// assert!(!v.is_empty());
200-
/// let mut so_far = 0u;
201-
/// for item in v.iter() {
202-
/// so_far += item.weight;
203-
/// if so_far > 100 {
204-
/// return *item;
205-
/// }
206+
/// ```rust
207+
/// fn foo(x: Option<int>) {
208+
/// match x {
209+
/// Some(n) if n >= 0 => println!("Some(Non-negative)"),
210+
/// Some(n) if n < 0 => println!("Some(Negative)"),
211+
/// Some(_) => unreachable!(), // compile error if commented out
212+
/// None => println!("None")
213+
/// }
214+
/// ```
215+
///
216+
/// Iterators:
217+
///
218+
/// ```rust
219+
/// fn divide_by_three(x: i32) -> i32 { // one of the poorest implementations of x/3
220+
/// for i in std::iter::count(0_i32, 1) {
221+
/// if i < 0 { panic!("i32 overflow"); }
222+
/// if x < 3*i { return i; }
206223
/// }
207-
/// // The above loop always returns, so we must hint to the
208-
/// // type checker that it isn't possible to get down here
224+
///
209225
/// unreachable!();
210226
/// }
211227
/// ```

0 commit comments

Comments
 (0)