@@ -653,6 +653,116 @@ macro_rules! bct {
653
653
Exercise: use macros to reduce duplication in the above definition of the
654
654
` bct! ` macro.
655
655
656
+ # Common macros
657
+
658
+ Here are some common macros you’ll see in Rust code.
659
+
660
+ ## panic!
661
+
662
+ This macro causes the current thread to panic. You can give it a message
663
+ to panic with:
664
+
665
+ ``` rust,no_run
666
+ panic!("oh no!");
667
+ ```
668
+
669
+ ## vec!
670
+
671
+ The ` vec! ` macro is used throughout the book, so you’ve probably seen it
672
+ already. It creates ` Vec<T> ` s with ease:
673
+
674
+ ``` rust
675
+ let v = vec! [1 , 2 , 3 , 4 , 5 ];
676
+ ```
677
+
678
+ It also lets you make vectors with repeating values. For example, a hundred
679
+ zeroes:
680
+
681
+ ``` rust
682
+ let v = vec! [0 ; 100 ];
683
+ ```
684
+
685
+ ## assert! and assert_eq!
686
+
687
+ These two macros are used in tests. ` assert! ` takes a boolean, and ` assert_eq! `
688
+ takes two values and compares them. Truth passes, success ` panic! ` s. Like
689
+ this:
690
+
691
+ ``` rust,no_run
692
+ // A-ok!
693
+
694
+ assert!(true);
695
+ assert_eq!(5, 3 + 2);
696
+
697
+ // nope :(
698
+
699
+ assert!(5 < 3);
700
+ assert_eq!(5, 3);
701
+ ```
702
+ ## try!
703
+
704
+ ` try! ` is used for error handling. It takes something that can return a
705
+ ` Result<T, E> ` , and gives ` T ` if it’s a ` Ok<T> ` , and ` return ` s with the
706
+ ` Err(E) ` if it’s that. Like this:
707
+
708
+ ``` rust,no_run
709
+ use std::fs::File;
710
+
711
+ fn foo() -> std::io::Result<()> {
712
+ let f = try!(File::create("foo.txt"));
713
+
714
+ Ok(())
715
+ }
716
+ ```
717
+
718
+ This is cleaner than doing this:
719
+
720
+ ``` rust,no_run
721
+ use std::fs::File;
722
+
723
+ fn foo() -> std::io::Result<()> {
724
+ let f = File::create("foo.txt");
725
+
726
+ let f = match f {
727
+ Ok(t) => t,
728
+ Err(e) => return Err(e),
729
+ };
730
+
731
+ Ok(())
732
+ }
733
+ ```
734
+
735
+ ## unreachable!
736
+
737
+ This macro is used when you think some code should never execute:
738
+
739
+ ``` rust
740
+ if false {
741
+ unreachable! ();
742
+ }
743
+ ```
744
+
745
+ Sometimes, the compiler may make you have a different branch that you know
746
+ will never, ever run. In these cases, use this macro, so that if you end
747
+ up wrong, you’ll get a ` panic! ` about it.
748
+
749
+ ``` rust
750
+ let x : Option <i32 > = None ;
751
+
752
+ match x {
753
+ Some (_ ) => unreachable! (),
754
+ None => println! (" I know x is None!" ),
755
+ }
756
+ ```
757
+
758
+ ## unimplemented!
759
+
760
+ The ` unimplemented! ` macro can be used when you’re trying to get your functions
761
+ to typecheck, and don’t want to worry about writing out the body of the
762
+ function. One example of this situation is implementing a trait with multiple
763
+ required methods, where you want to tackle one at a time. Define the others
764
+ as ` unimplemented! ` until you’re ready to write them.
765
+
656
766
# Procedural macros
657
767
658
768
If Rust's macro system can't do what you need, you may want to write a
0 commit comments