Skip to content

Commit dbbcb43

Browse files
committed
Add note about backtraces to TRPL
This is where panic!() is introduced. Fixes #27428
1 parent c6291e0 commit dbbcb43

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

src/doc/trpl/functions.md

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,61 @@ fn diverges() -> ! {
214214

215215
`panic!` is a macro, similar to `println!()` that we’ve already seen. Unlike
216216
`println!()`, `panic!()` causes the current thread of execution to crash with
217-
the given message.
217+
the given message. Because this function will cause a crash, it will never
218+
return, and so it has the type ‘`!`’, which is read ‘diverges’.
218219

219-
Because this function will cause a crash, it will never return, and so it has
220-
the type ‘`!`’, which is read ‘diverges’. A diverging function can be used
221-
as any type:
220+
If you add a main function that calls `diverges()` and run it, you’ll get
221+
some output that looks like this:
222+
223+
```text
224+
thread ‘<main>’ panicked at ‘This function never returns!’, hello.rs:2
225+
```
226+
227+
If you want more information, you can get a backtrace by setting the
228+
`RUST_BACKTRACE` environment variable:
229+
230+
```text
231+
$ RUST_BACKTRACE=1 ./diverges
232+
thread '<main>' panicked at 'This function never returns!', hello.rs:2
233+
stack backtrace:
234+
1: 0x7f402773a829 - sys::backtrace::write::h0942de78b6c02817K8r
235+
2: 0x7f402773d7fc - panicking::on_panic::h3f23f9d0b5f4c91bu9w
236+
3: 0x7f402773960e - rt::unwind::begin_unwind_inner::h2844b8c5e81e79558Bw
237+
4: 0x7f4027738893 - rt::unwind::begin_unwind::h4375279447423903650
238+
5: 0x7f4027738809 - diverges::h2266b4c4b850236beaa
239+
6: 0x7f40277389e5 - main::h19bb1149c2f00ecfBaa
240+
7: 0x7f402773f514 - rt::unwind::try::try_fn::h13186883479104382231
241+
8: 0x7f402773d1d8 - __rust_try
242+
9: 0x7f402773f201 - rt::lang_start::ha172a3ce74bb453aK5w
243+
10: 0x7f4027738a19 - main
244+
11: 0x7f402694ab44 - __libc_start_main
245+
12: 0x7f40277386c8 - <unknown>
246+
13: 0x0 - <unknown>
247+
```
248+
249+
`RUST_BACKTRACE` also works with Cargo’s `run` command:
250+
251+
```text
252+
$ RUST_BACKTRACE=1 cargo run
253+
Running `target/debug/diverges`
254+
thread '<main>' panicked at 'This function never returns!', hello.rs:2
255+
stack backtrace:
256+
1: 0x7f402773a829 - sys::backtrace::write::h0942de78b6c02817K8r
257+
2: 0x7f402773d7fc - panicking::on_panic::h3f23f9d0b5f4c91bu9w
258+
3: 0x7f402773960e - rt::unwind::begin_unwind_inner::h2844b8c5e81e79558Bw
259+
4: 0x7f4027738893 - rt::unwind::begin_unwind::h4375279447423903650
260+
5: 0x7f4027738809 - diverges::h2266b4c4b850236beaa
261+
6: 0x7f40277389e5 - main::h19bb1149c2f00ecfBaa
262+
7: 0x7f402773f514 - rt::unwind::try::try_fn::h13186883479104382231
263+
8: 0x7f402773d1d8 - __rust_try
264+
9: 0x7f402773f201 - rt::lang_start::ha172a3ce74bb453aK5w
265+
10: 0x7f4027738a19 - main
266+
11: 0x7f402694ab44 - __libc_start_main
267+
12: 0x7f40277386c8 - <unknown>
268+
13: 0x0 - <unknown>
269+
```
270+
271+
A diverging function can be used as any type:
222272

223273
```should_panic
224274
# fn diverges() -> ! {

0 commit comments

Comments
 (0)