Skip to content

Commit 9d408d9

Browse files
committed
Add todo!() macro
The use-case of `todo!()` macro is to be a much easier to type alternative to `unimplemented!()` macro.
1 parent 0f88167 commit 9d408d9

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/libcore/macros.rs

+59
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,65 @@ macro_rules! unimplemented {
559559
($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)*)));
560560
}
561561

562+
/// A standardized placeholder for marking unfinished code.
563+
///
564+
/// This can be useful if you are prototyping and are just looking to have your
565+
/// code typecheck. `todo!` works exactly like `unimplemented!`, there only
566+
/// difference between the two macros is the name.
567+
///
568+
/// # Panics
569+
///
570+
/// This will always [panic!](macro.panic.html)
571+
///
572+
/// # Examples
573+
///
574+
/// Here's an example of some in-progress code. We have a trait `Foo`:
575+
///
576+
/// ```
577+
/// trait Foo {
578+
/// fn bar(&self);
579+
/// fn baz(&self);
580+
/// }
581+
/// ```
582+
///
583+
/// We want to implement `Foo` on one of our types, but we also want to work on
584+
/// just `bar()` first. In order for our code to compile, we need to implement
585+
/// `baz()`, so we can use `todo!`:
586+
///
587+
/// ```
588+
/// #![feature(todo_macro)]
589+
///
590+
/// # trait Foo {
591+
/// # fn bar(&self);
592+
/// # fn baz(&self);
593+
/// # }
594+
/// struct MyStruct;
595+
///
596+
/// impl Foo for MyStruct {
597+
/// fn bar(&self) {
598+
/// // implementation goes here
599+
/// }
600+
///
601+
/// fn baz(&self) {
602+
/// // let's not worry about implementing baz() for now
603+
/// todo!();
604+
/// }
605+
/// }
606+
///
607+
/// fn main() {
608+
/// let s = MyStruct;
609+
/// s.bar();
610+
///
611+
/// // we aren't even using baz() yet, so this is fine.
612+
/// }
613+
/// ```
614+
#[macro_export]
615+
#[unstable(feature = "todo_macro", issue = "59277")]
616+
macro_rules! todo {
617+
() => (panic!("not yet implemented"));
618+
($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)*)));
619+
}
620+
562621
/// A macro to create an array of [`MaybeUninit`]
563622
///
564623
/// This macro constructs an uninitialized array of the type `[MaybeUninit<K>; N]`.

src/libstd/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@
301301
#![feature(stmt_expr_attributes)]
302302
#![feature(str_internals)]
303303
#![feature(thread_local)]
304+
#![feature(todo_macro)]
304305
#![feature(toowned_clone_into)]
305306
#![feature(try_reserve)]
306307
#![feature(unboxed_closures)]
@@ -323,7 +324,7 @@ use prelude::v1::*;
323324
#[stable(feature = "rust1", since = "1.0.0")]
324325
pub use core::{assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne};
325326
#[stable(feature = "rust1", since = "1.0.0")]
326-
pub use core::{unreachable, unimplemented, write, writeln, r#try};
327+
pub use core::{unreachable, unimplemented, write, writeln, r#try, todo};
327328

328329
#[allow(unused_imports)] // macros from `alloc` are not used on all platforms
329330
#[macro_use]

0 commit comments

Comments
 (0)