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.
37 /// This macro has a second version, where a custom panic message can be provided.
42 /// // the panic message for these assertions is the stringified value of the
43 /// // expression given.
46 /// fn some_computation() -> bool { true } // a very simple function
48 /// assert!(some_computation());
50 /// // assert with a custom message
52 /// assert!(x, "x wasn't true!");
54 /// let a = 3; let b = 27;
55 /// assert!(a + b == 30, "a = {}, b = {}", a, b);
58 #[stable(feature = "rust1", since = "1.0.0")]
62 panic!(concat!("assertion failed: ", stringify!($cond)))
65 ($cond:expr, $($arg:tt)+) => (
72 /// Asserts that two expressions are equal to each other.
74 /// On panic, this macro will print the values of the expressions with their
75 /// debug representations.
85 #[stable(feature = "rust1", since = "1.0.0")]
86 macro_rules! assert_eq {
87 ($left:expr , $right:expr) => ({
88 match (&($left), &($right)) {
89 (left_val, right_val) => {
90 if !(*left_val == *right_val) {
91 panic!("assertion failed: `(left == right)` \
92 (left: `{:?}`, right: `{:?}`)", *left_val, *right_val)
99 /// Ensure that a boolean expression is `true` at runtime.
101 /// This will invoke the `panic!` macro if the provided expression cannot be
102 /// evaluated to `true` at runtime.
104 /// Like `assert!`, this macro also has a second version, where a custom panic
105 /// message can be provided.
107 /// Unlike `assert!`, `debug_assert!` statements are only enabled in non
108 /// optimized builds by default. An optimized build will omit all
109 /// `debug_assert!` statements unless `-C debug-assertions` is passed to the
110 /// compiler. This makes `debug_assert!` useful for checks that are too
111 /// expensive to be present in a release build but may be helpful during
117 /// // the panic message for these assertions is the stringified value of the
118 /// // expression given.
119 /// debug_assert!(true);
121 /// fn some_expensive_computation() -> bool { true } // a very simple function
122 /// debug_assert!(some_expensive_computation());
124 /// // assert with a custom message
126 /// debug_assert!(x, "x wasn't true!");
128 /// let a = 3; let b = 27;
129 /// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
132 #[stable(feature = "rust1", since = "1.0.0")]
133 macro_rules! debug_assert {
134 ($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); })
137 /// Asserts that two expressions are equal to each other, testing equality in
140 /// On panic, this macro will print the values of the expressions.
142 /// Unlike `assert_eq!`, `debug_assert_eq!` statements are only enabled in non
143 /// optimized builds by default. An optimized build will omit all
144 /// `debug_assert_eq!` statements unless `-C debug-assertions` is passed to the
145 /// compiler. This makes `debug_assert_eq!` useful for checks that are too
146 /// expensive to be present in a release build but may be helpful during
154 /// debug_assert_eq!(a, b);
157 macro_rules! debug_assert_eq {
158 ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
161 /// Short circuiting evaluation on Err
163 /// `libstd` contains a more general `try!` macro that uses `From<E>`.
167 use $crate::result::Result::{Ok, Err};
171 Err(e) => return Err(e),
176 /// Use the `format!` syntax to write data into a buffer.
178 /// This macro is typically used with a buffer of `&mut `[`Write`][write].
180 /// See [`std::fmt`][fmt] for more information on format syntax.
182 /// [fmt]: fmt/index.html
183 /// [write]: io/trait.Write.html
188 /// use std::io::Write;
190 /// let mut w = Vec::new();
191 /// write!(&mut w, "test").unwrap();
192 /// write!(&mut w, "formatted {}", "arguments").unwrap();
194 /// assert_eq!(w, b"testformatted arguments");
198 ($dst:expr, $($arg:tt)*) => ($dst.write_fmt(format_args!($($arg)*)))
201 /// Use the `format!` syntax to write data into a buffer, appending a newline.
203 /// This macro is typically used with a buffer of `&mut `[`Write`][write].
205 /// See [`std::fmt`][fmt] for more information on format syntax.
207 /// [fmt]: fmt/index.html
208 /// [write]: io/trait.Write.html
213 /// use std::io::Write;
215 /// let mut w = Vec::new();
216 /// writeln!(&mut w, "test").unwrap();
217 /// writeln!(&mut w, "formatted {}", "arguments").unwrap();
219 /// assert_eq!(&w[..], "test\nformatted arguments\n".as_bytes());
222 #[stable(feature = "rust1", since = "1.0.0")]
223 macro_rules! writeln {
224 ($dst:expr, $fmt:expr) => (
225 write!($dst, concat!($fmt, "\n"))
227 ($dst:expr, $fmt:expr, $($arg:tt)*) => (
228 write!($dst, concat!($fmt, "\n"), $($arg)*)
232 /// A utility macro for indicating unreachable code.
234 /// This is useful any time that the compiler can't determine that some code is unreachable. For
237 /// * Match arms with guard conditions.
238 /// * Loops that dynamically terminate.
239 /// * Iterators that dynamically terminate.
243 /// This will always panic.
250 /// fn foo(x: Option<i32>) {
252 /// Some(n) if n >= 0 => println!("Some(Non-negative)"),
253 /// Some(n) if n < 0 => println!("Some(Negative)"),
254 /// Some(_) => unreachable!(), // compile error if commented out
255 /// None => println!("None")
263 /// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
265 /// if 3*i < i { panic!("u32 overflow"); }
266 /// if x < 3*i { return i-1; }
272 #[unstable(feature = "core",
273 reason = "relationship with panic is unclear",
275 macro_rules! unreachable {
277 panic!("internal error: entered unreachable code")
280 unreachable!("{}", $msg)
282 ($fmt:expr, $($arg:tt)*) => ({
283 panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
287 /// A standardized placeholder for marking unfinished code. It panics with the
288 /// message `"not yet implemented"` when executed.
290 /// This can be useful if you are prototyping and are just looking to have your
291 /// code typecheck, or if you're implementing a trait that requires multiple
292 /// methods, and you're only planning on using one of them.
296 /// Here's an example of some in-progress code. We have a trait `Foo`:
305 /// We want to implement `Foo` on one of our types, but we also want to work on
306 /// just `bar()` first. In order for our code to compile, we need to implement
307 /// `baz()`, so we can use `unimplemented!`:
316 /// impl Foo for MyStruct {
318 /// // implementation goes here
322 /// // let's not worry about implementing bar() for now
323 /// unimplemented!();
328 /// let s = MyStruct;
331 /// // we aren't even using bar() yet, so this is fine.
335 #[unstable(feature = "core",
336 reason = "relationship with panic is unclear",
338 macro_rules! unimplemented {
339 () => (panic!("not yet implemented"))