rust-lang / rust-lang/rust

Suggest `parse` on expected types that implement `FromStr`

Open
#159,494 6 comments 0 reactions 1 assignee View on GitHub

@Snxhit is already working on this.

Since Jul 28, 2026.

A-diagnostics D-lack-of-suggestion D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn main() {
    let port: u16 = "8080";
    println!("{port}");
    let text = String::from("42");
    let number = text as i32;
}
Current output
error[E0308]: mismatched types
  --> $DIR/string-literal-assigned-to-parsable-type.rs:11:21
   |
LL |     let port: u16 = "8080";
   |               ---   ^^^^^^ expected `u16`, found `&str`
   |               |
   |               expected due to this

error[E0605]: non-primitive cast: `String` as `i32`
  --> $DIR/string-to-number-with-as.rs:10:18
   |
LL |     let number = text as i32;
   |                  ^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
Desired output
Suggest `"8080".parse<i16>().unwrap()` and `let number = text.parse::<i32>().unwrap()`.

For the former, detect literal could have been a number to begin with.
Rationale and extra context

No response

Other cases

Rust Version
1.97
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.