chainerror/booksrc/tutorial1.md

26 lines
761 B
Markdown
Raw Normal View History

2018-12-20 16:37:08 +01:00
# Simple String Errors
2018-12-20 15:14:21 +01:00
2018-12-21 13:50:08 +01:00
An easy way of doing error handling in rust is by returning `String` as a `Box<std::error::Error>`.
2018-12-20 15:14:21 +01:00
2018-12-21 13:50:08 +01:00
If the rust `main` function returns an `Err()`, this `Err()` will be displayed with `std::fmt::Debug`.
As you can see by running the example (by pressing the "Play" button in upper right of the code block),
this only
2018-12-20 16:36:04 +01:00
prints out the last `Error`.
2018-12-20 15:14:21 +01:00
2018-12-21 13:50:08 +01:00
~~~
Error: StringError("func1 error")
~~~
The next chapters of this tutorial show how `chainerror` adds more information
and improves inspecting the sources of an error.
You can also run the tutorial examples in the checked out
[chainerror git repo](https://github.com/haraldh/chainerror).
~~~
$ cargo run -q --example tutorial1
~~~
2018-12-20 15:14:21 +01:00
2018-12-20 14:52:06 +01:00
~~~rust
2018-12-21 13:50:08 +01:00
{{#include ../examples/tutorial1.rs}}
2018-12-20 14:52:06 +01:00
~~~