Skip to content

Commit a2ccc81

Browse files
committed
remove example usage of from_str in error docs
Fixes #24185
1 parent 3ca83a7 commit a2ccc81

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/doc/trpl/error-handling.md

+18-15
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ panic. A *failure* is an error that can be recovered from in some way. A
2020
*panic* is an error that cannot be recovered from.
2121

2222
What do we mean by "recover"? Well, in most cases, the possibility of an error
23-
is expected. For example, consider the `from_str` function:
23+
is expected. For example, consider the `parse` function:
2424

25-
```{rust,ignore}
26-
from_str("5");
25+
```ignore
26+
"5".parse();
2727
```
2828

29-
This function takes a string argument and converts it into another type. But
30-
because it's a string, you can't be sure that the conversion actually works.
31-
For example, what should this convert to?
29+
This method converts a string into another type. But because it's a string, you
30+
can't be sure that the conversion actually works. For example, what should this
31+
convert to?
3232

33-
```{rust,ignore}
34-
from_str("hello5world");
33+
```ignore
34+
"hello5world".parse();
3535
```
3636

3737
This won't work. So we know that this function will only work properly for some
@@ -40,7 +40,8 @@ inputs. It's expected behavior. We call this kind of error a *failure*.
4040
On the other hand, sometimes, there are errors that are unexpected, or which
4141
we cannot recover from. A classic example is an `assert!`:
4242

43-
```{rust,ignore}
43+
```rust
44+
# let x = 5;
4445
assert!(x == 5);
4546
```
4647

@@ -119,17 +120,19 @@ Rust calls these sorts of errors *panics*.
119120
# Handling errors with `Option` and `Result`
120121

121122
The simplest way to indicate that a function may fail is to use the `Option<T>`
122-
type. Remember our `from_str()` example? Here's its type signature:
123+
type. For example, the `find` method on strings attempts to find a pattern
124+
in a string, and returns an `Option`:
123125

124-
```{rust,ignore}
125-
pub fn from_str<A: FromStr>(s: &str) -> Option<A>
126+
```rust
127+
let s = "foo";
128+
129+
assert_eq!(s.find('f'), Some(0));
130+
assert_eq!(s.find('z'), None);
126131
```
127132

128-
`from_str()` returns an `Option<A>`. If the conversion succeeds, it will return
129-
`Some(value)`, and if it fails, it will return `None`.
130133

131134
This is appropriate for the simplest of cases, but doesn't give us a lot of
132-
information in the failure case. What if we wanted to know _why_ the conversion
135+
information in the failure case. What if we wanted to know _why_ the function
133136
failed? For this, we can use the `Result<T, E>` type. It looks like this:
134137

135138
```rust

0 commit comments

Comments
 (0)