diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-11-26 09:46:57 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-11-26 16:50:12 -0800 |
| commit | 8999a7987cbd6ea7ea4c5a53af178de9c1b42c52 (patch) | |
| tree | 5a74a9889141904bb9f8078fcf33b535d1d075ac /src/libstd | |
| parent | 52ca9523a551fa6fa8a9ab1ddd35714a0b0c3fc6 (diff) | |
| parent | 4ce3ba484b00b6a8e2db63f61c8944e0b33b07ff (diff) | |
| download | rust-8999a7987cbd6ea7ea4c5a53af178de9c1b42c52.tar.gz rust-8999a7987cbd6ea7ea4c5a53af178de9c1b42c52.zip | |
rollup merge of #19316: steveklabnik/gh18876
Fixes #18876
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/macros.rs | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index c3260225d0a..dbb45f2e556 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -186,26 +186,42 @@ macro_rules! debug_assert_eq( ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); }) ) -/// A utility macro for indicating unreachable code. It will panic if -/// executed. This is occasionally useful to put after loops that never -/// terminate normally, but instead directly return from a function. +/// A utility macro for indicating unreachable code. /// -/// # Example +/// This is useful any time that the compiler can't determine that some code is unreachable. For +/// example: +/// +/// * Match arms with guard conditions. +/// * Loops that dynamically terminate. +/// * Iterators that dynamically terminate. +/// +/// # Panics +/// +/// This will always panic. +/// +/// # Examples +/// +/// Match arms: /// -/// ```{.rust} -/// struct Item { weight: uint } -/// -/// fn choose_weighted_item(v: &[Item]) -> Item { -/// assert!(!v.is_empty()); -/// let mut so_far = 0u; -/// for item in v.iter() { -/// so_far += item.weight; -/// if so_far > 100 { -/// return *item; -/// } +/// ```rust +/// fn foo(x: Option<int>) { +/// match x { +/// Some(n) if n >= 0 => println!("Some(Non-negative)"), +/// Some(n) if n < 0 => println!("Some(Negative)"), +/// Some(_) => unreachable!(), // compile error if commented out +/// None => println!("None") +/// } +/// ``` +/// +/// 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; } /// } -/// // The above loop always returns, so we must hint to the -/// // type checker that it isn't possible to get down here +/// /// unreachable!(); /// } /// ``` |
