Where is i32.from_str?

I am trying to do an exercise which requires "string is parsed using the i32::from_str method" and I found the trait implementation in the docs: from_str. But I get this error when I try to use it like:

4 |     let result = int.from_str(str);
  |                      ^^^^^^^^ method not found in `i32`

What am I doing wrong here?

The syntax of foo.bar(…) is only for variables foo. For a type foo, you’ll have to use :: as a separator. Unless int is already a variable… but note that for a string->int conversion you don’t need any pre-existing integer, instead you’re trying to create a new one, …right?


The error message isn’t too helpful unfortunately. It seems like your code was too far from working for useful lints to appear. For example, if you did write i32::from_str then you would have gotten diagnostics such as

error[E0599]: no function or associated item named `from_str` found for type `i32` in the current scope
 --> src/lib.rs:6:18
  |
6 |     let y = i32::from_str(str);
  |                  ^^^^^^^^ function or associated item not found in `i32`
  |
  = help: items from traits can only be used if the trait is in scope
help: trait `FromStr` which provides `from_str` is implemented but not in scope; perhaps you want to import it
  |
3 + use std::str::FromStr;
  |
help: there is an associated function `from` with a similar name
  |
6 |     let y = i32::from(str);
  |                  ~~~~

suggesting the second missing piece for a trait method: bringing the trait itself into scope with

use std::str::FromStr;

If instead you had already had that use statement, then diagnostics appear that suggest the i32::from_str syntax, as in:

error[E0599]: no method named `from_str` found for type `i32` in the current scope
   --> src/lib.rs:6:15
    |
6   |     let y = i.from_str(str);
    |             --^^^^^^^^-----
    |             | |
    |             | this is an associated function, not a method
    |             help: use associated function syntax instead: `i32::from_str(str)`
    |
    = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in the trait `FromStr`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/str/traits.rs:823:5
    |
823 |     fn from_str(s: &str) -> Result<Self, Self::Err>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You’ve almost gotten to the right place already… there is a “read more” button on that method, and it takes you to the method’s documentation on the trait FromStr page, where it comes with some very useful example code!

Examples

Basic usage with i32, a type that implements FromStr:

use std::str::FromStr;

let s = "5";
let x = i32::from_str(s).unwrap();

assert_eq!(5, x);

On that same page, there’s additional helpful stuff: For example

FromStr’s from_str method is often used implicitly, through str’s parse method. See parse’s documentation for examples.

Which – that’s a cool thing, isn’t it? You could have just written

let result: Result<i32, _> = str.parse();
// this type ^^^^^^^^^^^^ annotation might be redundant in other use-cases
// where type inference can figure out the desired type based on usage

or

let result = str.parse::<i32>();
// this way of explicitly ^^^ passing the type parameter
// can be more light-weight than the full `Result<i32, _>` annotation
// for cases where you *do* end up needing to assign this information

instead (assuming str is a variable that holds some string value); without needing the use …:FromStr; even. Reading in more corners of the std docs can be quite educational.

7 Likes

Or even - in the context of a function returning Result:

let value: i32 = str.parse()?;
2 Likes