Skip to content

Commit b7091e4

Browse files
rewrite documentation for unimplemented!
1 parent 66bf391 commit b7091e4

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/libcore/macros.rs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -519,54 +519,71 @@ macro_rules! unreachable {
519519
});
520520
}
521521

522-
/// Indicates unfinished code.
522+
/// Indicates unfinished code by panicking with a message of "not yet implemented".
523523
///
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.
527526
///
528527
/// # Panics
529528
///
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.
531533
///
532534
/// # Examples
533535
///
534536
/// Here's an example of some in-progress code. We have a trait `Foo`:
535537
///
536538
/// ```
537539
/// trait Foo {
538-
/// fn bar(&self);
540+
/// fn bar(&self) -> u8;
539541
/// fn baz(&self);
542+
/// fn qux(&self) -> Result<u64, ()>;
540543
/// }
541544
/// ```
542545
///
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.
546553
///
547554
/// ```
548555
/// # trait Foo {
549-
/// # fn bar(&self);
556+
/// # fn bar(&self) -> u8;
550557
/// # fn baz(&self);
558+
/// # fn qux(&self) -> Result<u64, ()>;
551559
/// # }
552560
/// struct MyStruct;
553561
///
554562
/// impl Foo for MyStruct {
555-
/// fn bar(&self) {
556-
/// // implementation goes here
563+
/// fn bar(&self) -> u8 {
564+
/// 1 + 1
557565
/// }
558566
///
559567
/// 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'".
561571
/// unimplemented!();
562572
/// }
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+
/// }
563582
/// }
564583
///
565584
/// fn main() {
566585
/// let s = MyStruct;
567586
/// s.bar();
568-
///
569-
/// // we aren't even using baz() yet, so this is fine.
570587
/// }
571588
/// ```
572589
#[macro_export]

0 commit comments

Comments
 (0)