# Better diagnostic informations for match an \`Option\<String\>\` with \`Option\<&str\>\`

**URL:** <https://internals.rust-lang.org/t/better-diagnostic-informations-for-match-an-option-string-with-option-str/21738>\
**Category:** language design\
**Created:** [October 22, 2024, 4:12am UTC](https://internals.rust-lang.org/t/better-diagnostic-informations-for-match-an-option-string-with-option-str/21738 "2024-10-22T04:12:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neutron3529](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/neutron3529/32/6976_2.png) [@Neutron3529](https://internals.rust-lang.org/u/Neutron3529)\
**Post date:** [October 22, 2024, 4:12am UTC](https://internals.rust-lang.org/t/better-diagnostic-informations-for-match-an-option-string-with-option-str/21738/1 "2024-10-22T04:12:40Z")

</div>

When I tried

```rust
match std::env::args().nth(1) {
    Some("foo") =>(),
    _ => ()
}

```

A very disappointed information occurs:

```rust
error[E0308]: mismatched types
 --> test.rs:3:10
  |
2 | match std::env::args().nth(1) {
  | ----------------------- this expression has type `Option<String>`
3 | Some("foo") =>(),
  | ^^^^^ expected `String`, found `&str`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

```

The correct version should be

```rust
match std::env::args().nth(1).as_deref() {
    Some("foo") =>(),
    _ => ()
}

```

It takes a lot of time to figure out what the correct function is:

```rust
std::env::args().nth(1).as_ref() // Got Option<&String>, but not Option<&str>
std::env::args().nth(1).map(|x|&x) // trying map Option<String> to Option<&str>, but failed since `returns a value referencing data owned by the current function` is not allowed
std::env::args().nth(1).as_ref().map(|x|x.as_str()) // if `as_deref` does not came into mind, this should be the only way.

```

All the methods are not easy directly enough, thus we may need another error messsage about that, to tell the users how to match an `Option<String>` with `Option<&str>`.

Same things may happened to something like `Result<String, String>`, either.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [April 15, 2026, 4:12am UTC](https://internals.rust-lang.org/t/better-diagnostic-informations-for-match-an-option-string-with-option-str/21738/2 "2026-04-15T04:12:55Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
