@@ -186,26 +186,42 @@ macro_rules! debug_assert_eq(
186
186
( $( $arg: tt) * ) => ( if cfg!( not( ndebug) ) { assert_eq!( $( $arg) * ) ; } )
187
187
)
188
188
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.
192
190
///
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:
194
205
///
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; }
206
223
/// }
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
+ ///
209
225
/// unreachable!();
210
226
/// }
211
227
/// ```
0 commit comments