Skip to content

Commit 2f0618d

Browse files
authored
Rollup merge of rust-lang#64726 - andrewbanchich:unimplemented, r=rkruppe
rewrite documentation for unimplemented! to clarify use The current docs for `unimplemented!` seem to miss the point of this macro. > This can be useful if you are prototyping and are just looking to have your code type-check, or if you're implementing a trait that requires multiple methods, and you're only planning on using one of them. You could also return a `()` if you just want your code to type-check. I think `unimplemented!` is useful for when you want your program to exit when it reaches an unimplemented area. I rewrote the explanation and gave examples of both forms of this macro that I think clarify its use a little better.
2 parents c20654e + b7091e4 commit 2f0618d

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/libcore/macros.rs

+32-15
Original file line numberDiff line numberDiff line change
@@ -520,57 +520,74 @@ macro_rules! unreachable {
520520
});
521521
}
522522

523-
/// Indicates unfinished code.
523+
/// Indicates unfinished code by panicking with a message of "not yet implemented".
524524
///
525-
/// This can be useful if you are prototyping and are just looking to have your
526-
/// code type-check, or if you're implementing a trait that requires multiple
527-
/// methods, and you're only planning on using one of them.
525+
/// This allows the your code to type-check, which is useful if you are prototyping or
526+
/// implementing a trait that requires multiple methods which you don't plan of using all of.
528527
///
529528
/// There is no difference between `unimplemented!` and `todo!` apart from the
530529
/// name.
531530
///
532531
/// # Panics
533532
///
534-
/// This will always [panic!](macro.panic.html)
533+
/// This will always [panic!](macro.panic.html) because `unimplemented!` is just a
534+
/// shorthand for `panic!` with a fixed, specific message.
535+
///
536+
/// Like `panic!`, this macro has a second form for displaying custom values.
535537
///
536538
/// # Examples
537539
///
538540
/// Here's an example of some in-progress code. We have a trait `Foo`:
539541
///
540542
/// ```
541543
/// trait Foo {
542-
/// fn bar(&self);
544+
/// fn bar(&self) -> u8;
543545
/// fn baz(&self);
546+
/// fn qux(&self) -> Result<u64, ()>;
544547
/// }
545548
/// ```
546549
///
547-
/// We want to implement `Foo` on one of our types, but we also want to work on
548-
/// just `bar()` first. In order for our code to compile, we need to implement
549-
/// `baz()`, so we can use `unimplemented!`:
550+
/// We want to implement `Foo` for 'MyStruct', but so far we only know how to
551+
/// implement the `bar()` function. `baz()` and `qux()` will still need to be defined
552+
/// in our implementation of `Foo`, but we can use `unimplemented!` in their definitions
553+
/// to allow our code to compile.
554+
///
555+
/// In the meantime, we want to have our program stop running once these
556+
/// unimplemented functions are reached.
550557
///
551558
/// ```
552559
/// # trait Foo {
553-
/// # fn bar(&self);
560+
/// # fn bar(&self) -> u8;
554561
/// # fn baz(&self);
562+
/// # fn qux(&self) -> Result<u64, ()>;
555563
/// # }
556564
/// struct MyStruct;
557565
///
558566
/// impl Foo for MyStruct {
559-
/// fn bar(&self) {
560-
/// // implementation goes here
567+
/// fn bar(&self) -> u8 {
568+
/// 1 + 1
561569
/// }
562570
///
563571
/// fn baz(&self) {
564-
/// // let's not worry about implementing baz() for now
572+
/// // We aren't sure how to even start writing baz yet,
573+
/// // so we have no logic here at all.
574+
/// // This will display "thread 'main' panicked at 'not yet implemented'".
565575
/// unimplemented!();
566576
/// }
577+
///
578+
/// fn qux(&self) -> Result<u64, ()> {
579+
/// let n = self.bar();
580+
/// // We have some logic here,
581+
/// // so we can use unimplemented! to display what we have so far.
582+
/// // This will display:
583+
/// // "thread 'main' panicked at 'not yet implemented: we need to divide by 2'".
584+
/// unimplemented!("we need to divide by {}", n);
585+
/// }
567586
/// }
568587
///
569588
/// fn main() {
570589
/// let s = MyStruct;
571590
/// s.bar();
572-
///
573-
/// // we aren't even using baz() yet, so this is fine.
574591
/// }
575592
/// ```
576593
#[macro_export]

0 commit comments

Comments
 (0)