Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added details about parse and expect methods #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Common Programming Concepts/Basic Data Types/Intro/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ Keep in mind that Rust is a _statically typed language_, which means that it mus
let guess: u32 = "42".parse().expect("Not a number!");
```

<div class="hint" title="Code explanation">

Although the example is focused on type annotation, it uses the `parse` and `expect` methods.
We won't pay much attention to them now, but for a general understanding, they are used for the following:
- `parse` converts a string to another type (number in this instance) and returns either a value or `Err`;
- `expect` allows you to set a custom error message for panic in case `parse` returns `Err`.

For example, this code
```rust
let guess: u32 = "a42".parse().expect("Not a number!");
```
will cause the following error:
```text
thread 'main' panicked at Common Programming Concepts/Basic Data Types/Intro/src/main.rs:2:36:
Not a number!: ParseIntError { kind: InvalidDigit }
...
```
</div>


If we don’t add the type annotation here, Rust will display the following error, which means the compiler needs more information from us to know which type we want to use:

```text
Expand Down