Skip to content

Commit

Permalink
Use conventional naming for fallible function enums sample (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
cassidoxa authored Mar 20, 2024
1 parent befd764 commit d7408f0
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/language/custom-types/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ value). Instead, it's up to the developer to provide such a helper function:

```rust
impl DayOfWeek {
fn from_i32(n: i32) -> Result<DayOfWeek, i32> {
fn try_from_i32(n: i32) -> Result<DayOfWeek, i32> {
use DayOfWeek::*;
match n {
0 => Ok(Sunday),
Expand All @@ -94,15 +94,15 @@ impl DayOfWeek {
}
```

The `from_i32` function returns a `DayOfWeek` in a `Result` indicating success
The `try_from_i32` function returns a `DayOfWeek` in a `Result` indicating success
(`Ok`) if `n` is valid. Otherwise it returns `n` as-is in a `Result`
indicating failure (`Err`):

```rust
let dow = DayOfWeek::from_i32(5);
let dow = DayOfWeek::try_from_i32(5);
println!("{dow:?}"); // prints: Ok(Friday)

let dow = DayOfWeek::from_i32(50);
let dow = DayOfWeek::try_from_i32(50);
println!("{dow:?}"); // prints: Err(50)
```

Expand Down

0 comments on commit d7408f0

Please sign in to comment.