1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /// Entry point of thread panic, for details, see std::macros
13 #[allow_internal_unstable]
16 panic!("explicit panic")
19 static _MSG_FILE_LINE: (&'static str, &'static str, u32) = ($msg, file!(), line!());
20 $crate::panicking::panic(&_MSG_FILE_LINE)
22 ($fmt:expr, $($arg:tt)*) => ({
23 // The leading _'s are to avoid dead code warnings if this is
24 // used inside a dead function. Just `#[allow(dead_code)]` is
25 // insufficient, since the user may have
26 // `#[forbid(dead_code)]` and which cannot be overridden.
27 static _FILE_LINE: (&'static str, u32) = (file!(), line!());
28 $crate::panicking::panic_fmt(format_args!($fmt, $($arg)*), &_FILE_LINE)
32 /// Ensure that a boolean expression is `true` at runtime.
34 /// This will invoke the `panic!` macro if the provided expression cannot be
35 /// evaluated to `true` at runtime.
40 /// // the panic message for these assertions is the stringified value of the
41 /// // expression given.
44 /// fn some_computation() -> bool { true } // a very simple function
46 /// assert!(some_computation());
48 /// // assert with a custom message
50 /// assert!(x, "x wasn't true!");
52 /// let a = 3; let b = 27;
53 /// assert!(a + b == 30, "a = {}, b = {}", a, b);
56 #[stable(feature = "rust1", since = "1.0.0")]
60 panic!(concat!("assertion failed: ", stringify!($cond)))
63 ($cond:expr, $($arg:tt)+) => (
70 /// Asserts that two expressions are equal to each other.
72 /// On panic, this macro will print the values of the expressions with their
73 /// debug representations.
83 #[stable(feature = "rust1", since = "1.0.0")]
84 macro_rules! assert_eq {
85 ($left:expr , $right:expr) => ({
86 match (&($left), &($right)) {
87 (left_val, right_val) => {
88 if !(*left_val == *right_val) {
89 panic!("assertion failed: `(left == right)` \
90 (left: `{:?}`, right: `{:?}`)", *left_val, *right_val)
97 /// Ensure that a boolean expression is `true` at runtime.
99 /// This will invoke the `panic!` macro if the provided expression cannot be
100 /// evaluated to `true` at runtime.
102 /// Unlike `assert!`, `debug_assert!` statements are only enabled in non
103 /// optimized builds by default. An optimized build will omit all
104 /// `debug_assert!` statements unless `-C debug-assertions` is passed to the
105 /// compiler. This makes `debug_assert!` useful for checks that are too
106 /// expensive to be present in a release build but may be helpful during
112 /// // the panic message for these assertions is the stringified value of the
113 /// // expression given.
114 /// debug_assert!(true);
116 /// fn some_expensive_computation() -> bool { true } // a very simple function
117 /// debug_assert!(some_expensive_computation());
119 /// // assert with a custom message
121 /// debug_assert!(x, "x wasn't true!");
123 /// let a = 3; let b = 27;
124 /// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
127 #[stable(feature = "rust1", since = "1.0.0")]
128 macro_rules! debug_assert {
129 ($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); })
132 /// Asserts that two expressions are equal to each other, testing equality in
135 /// On panic, this macro will print the values of the expressions.
137 /// Unlike `assert_eq!`, `debug_assert_eq!` statements are only enabled in non
138 /// optimized builds by default. An optimized build will omit all
139 /// `debug_assert_eq!` statements unless `-C debug-assertions` is passed to the
140 /// compiler. This makes `debug_assert_eq!` useful for checks that are too
141 /// expensive to be present in a release build but may be helpful during
149 /// debug_assert_eq!(a, b);
152 macro_rules! debug_assert_eq {
153 ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
156 /// Short circuiting evaluation on Err
158 /// `libstd` contains a more general `try!` macro that uses `From<E>`.
162 use $crate::result::Result::{Ok, Err};
166 Err(e) => return Err(e),
171 /// Use the `format!` syntax to write data into a buffer.
173 /// This macro is typically used with a buffer of `&mut `[`Write`][write].
175 /// See [`std::fmt`][fmt] for more information on format syntax.
177 /// [fmt]: fmt/index.html
178 /// [write]: io/trait.Write.html
183 /// use std::io::Write;
185 /// let mut w = Vec::new();
186 /// write!(&mut w, "test").unwrap();
187 /// write!(&mut w, "formatted {}", "arguments").unwrap();
189 /// assert_eq!(w, b"testformatted arguments");
193 ($dst:expr, $($arg:tt)*) => ($dst.write_fmt(format_args!($($arg)*)))
196 /// Use the `format!` syntax to write data into a buffer, appending a newline.
198 /// This macro is typically used with a buffer of `&mut `[`Write`][write].
200 /// See [`std::fmt`][fmt] for more information on format syntax.
202 /// [fmt]: fmt/index.html
203 /// [write]: io/trait.Write.html
208 /// use std::io::Write;
210 /// let mut w = Vec::new();
211 /// writeln!(&mut w, "test").unwrap();
212 /// writeln!(&mut w, "formatted {}", "arguments").unwrap();
214 /// assert_eq!(&w[..], "test\nformatted arguments\n".as_bytes());
217 #[stable(feature = "rust1", since = "1.0.0")]
218 macro_rules! writeln {
219 ($dst:expr, $fmt:expr) => (
220 write!($dst, concat!($fmt, "\n"))
222 ($dst:expr, $fmt:expr, $($arg:tt)*) => (
223 write!($dst, concat!($fmt, "\n"), $($arg)*)
227 /// A utility macro for indicating unreachable code.
229 /// This is useful any time that the compiler can't determine that some code is unreachable. For
232 /// * Match arms with guard conditions.
233 /// * Loops that dynamically terminate.
234 /// * Iterators that dynamically terminate.
238 /// This will always panic.
245 /// fn foo(x: Option<i32>) {
247 /// Some(n) if n >= 0 => println!("Some(Non-negative)"),
248 /// Some(n) if n < 0 => println!("Some(Negative)"),
249 /// Some(_) => unreachable!(), // compile error if commented out
250 /// None => println!("None")
258 /// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
260 /// if 3*i < i { panic!("u32 overflow"); }
261 /// if x < 3*i { return i-1; }
267 #[unstable(feature = "core",
268 reason = "relationship with panic is unclear",
270 macro_rules! unreachable {
272 panic!("internal error: entered unreachable code")
275 unreachable!("{}", $msg)
277 ($fmt:expr, $($arg:tt)*) => ({
278 panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
282 /// A standardised placeholder for marking unfinished code. It panics with the
283 /// message `"not yet implemented"` when executed.
285 /// This can be useful if you are prototyping and are just looking to have your
286 /// code typecheck, or if you're implementing a trait that requires multiple
287 /// methods, and you're only planning on using one of them.
291 /// Here's an example of some in-progress code. We have a trait `Foo`:
300 /// We want to implement `Foo` on one of our types, but we also want to work on
301 /// just `bar()` first. In order for our code to compile, we need to implement
302 /// `baz()`, so we can use `unimplemented!`:
311 /// impl Foo for MyStruct {
313 /// // implementation goes here
317 /// // let's not worry about implementing bar() for now
318 /// unimplemented!();
323 /// let s = MyStruct;
326 /// // we aren't even using bar() yet, so this is fine.
330 #[unstable(feature = "core",
331 reason = "relationship with panic is unclear",
333 macro_rules! unimplemented {
334 () => (panic!("not yet implemented"))