]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs
Rollup merge of #86415 - Kmeakin:iterator-associativity-docs, r=dtolnay
[rust.git] / src / test / ui / rfc-2361-dbg-macro / dbg-macro-expected-behavior.rs
1 // run-pass
2 // check-run-results
3
4 // Tests ensuring that `dbg!(expr)` has the expected run-time behavior.
5 // as well as some compile time properties we expect.
6
7 #[derive(Copy, Clone, Debug)]
8 struct Unit;
9
10 #[derive(Copy, Clone, Debug, PartialEq)]
11 struct Point<T> {
12     x: T,
13     y: T,
14 }
15
16 #[derive(Debug, PartialEq)]
17 struct NoCopy(usize);
18
19 fn main() {
20     let a: Unit = dbg!(Unit);
21     let _: Unit = dbg!(a);
22     // We can move `a` because it's Copy.
23     drop(a);
24
25     // `Point<T>` will be faithfully formatted according to `{:#?}`.
26     let a = Point { x: 42, y: 24 };
27     let b: Point<u8> = dbg!(Point { x: 42, y: 24 }); // test stringify!(..)
28     let c: Point<u8> = dbg!(b);
29     // Identity conversion:
30     assert_eq!(a, b);
31     assert_eq!(a, c);
32     // We can move `b` because it's Copy.
33     drop(b);
34
35     // Without parameters works as expected.
36     let _: () = dbg!();
37
38     // Test that we can borrow and that successive applications is still identity.
39     let a = NoCopy(1337);
40     let b: &NoCopy = dbg!(dbg!(&a));
41     assert_eq!(&a, b);
42
43     // Test involving lifetimes of temporaries:
44     fn f<'a>(x: &'a u8) -> &'a u8 { x }
45     let a: &u8 = dbg!(f(&42));
46     assert_eq!(a, &42);
47
48     // Test side effects:
49     let mut foo = 41;
50     assert_eq!(7331, dbg!({
51         foo += 1;
52         eprintln!("before");
53         7331
54     }));
55     assert_eq!(foo, 42);
56
57     // Test trailing comma:
58     assert_eq!(("Yeah",), dbg!(("Yeah",)));
59
60     // Test multiple arguments:
61     assert_eq!((1u8, 2u32), dbg!(1,
62                                  2));
63
64     // Test multiple arguments + trailing comma:
65     assert_eq!((1u8, 2u32, "Yeah"), dbg!(1u8, 2u32,
66                                          "Yeah",));
67 }