@@ -519,54 +519,71 @@ macro_rules! unreachable {
519
519
} ) ;
520
520
}
521
521
522
- /// Indicates unfinished code.
522
+ /// Indicates unfinished code by panicking with a message of "not yet implemented" .
523
523
///
524
- /// This can be useful if you are prototyping and are just looking to have your
525
- /// code type-check, or if you're implementing a trait that requires multiple
526
- /// methods, and you're only planning on using one of them.
524
+ /// This allows the your code to type-check, which is useful if you are prototyping or
525
+ /// implementing a trait that requires multiple methods which you don't plan of using all of.
527
526
///
528
527
/// # Panics
529
528
///
530
- /// This will always [panic!](macro.panic.html)
529
+ /// This will always [panic!](macro.panic.html) because `unimplemented!` is just a
530
+ /// shorthand for `panic!` with a fixed, specific message.
531
+ ///
532
+ /// Like `panic!`, this macro has a second form for displaying custom values.
531
533
///
532
534
/// # Examples
533
535
///
534
536
/// Here's an example of some in-progress code. We have a trait `Foo`:
535
537
///
536
538
/// ```
537
539
/// trait Foo {
538
- /// fn bar(&self);
540
+ /// fn bar(&self) -> u8 ;
539
541
/// fn baz(&self);
542
+ /// fn qux(&self) -> Result<u64, ()>;
540
543
/// }
541
544
/// ```
542
545
///
543
- /// We want to implement `Foo` on one of our types, but we also want to work on
544
- /// just `bar()` first. In order for our code to compile, we need to implement
545
- /// `baz()`, so we can use `unimplemented!`:
546
+ /// We want to implement `Foo` for 'MyStruct', but so far we only know how to
547
+ /// implement the `bar()` function. `baz()` and `qux()` will still need to be defined
548
+ /// in our implementation of `Foo`, but we can use `unimplemented!` in their definitions
549
+ /// to allow our code to compile.
550
+ ///
551
+ /// In the meantime, we want to have our program stop running once these
552
+ /// unimplemented functions are reached.
546
553
///
547
554
/// ```
548
555
/// # trait Foo {
549
- /// # fn bar(&self);
556
+ /// # fn bar(&self) -> u8 ;
550
557
/// # fn baz(&self);
558
+ /// # fn qux(&self) -> Result<u64, ()>;
551
559
/// # }
552
560
/// struct MyStruct;
553
561
///
554
562
/// impl Foo for MyStruct {
555
- /// fn bar(&self) {
556
- /// // implementation goes here
563
+ /// fn bar(&self) -> u8 {
564
+ /// 1 + 1
557
565
/// }
558
566
///
559
567
/// fn baz(&self) {
560
- /// // let's not worry about implementing baz() for now
568
+ /// // We aren't sure how to even start writing baz yet,
569
+ /// // so we have no logic here at all.
570
+ /// // This will display "thread 'main' panicked at 'not yet implemented'".
561
571
/// unimplemented!();
562
572
/// }
573
+ ///
574
+ /// fn qux(&self) -> Result<u64, ()> {
575
+ /// let n = self.bar();
576
+ /// // We have some logic here,
577
+ /// // so we can use unimplemented! to display what we have so far.
578
+ /// // This will display:
579
+ /// // "thread 'main' panicked at 'not yet implemented: we need to divide by 2'".
580
+ /// unimplemented!("we need to divide by {}", n);
581
+ /// }
563
582
/// }
564
583
///
565
584
/// fn main() {
566
585
/// let s = MyStruct;
567
586
/// s.bar();
568
- ///
569
- /// // we aren't even using baz() yet, so this is fine.
570
587
/// }
571
588
/// ```
572
589
#[ macro_export]
0 commit comments