Syntactic Fructose
Syntactic Fructose

Reputation: 20076

Unresolved import from_str

How do I use from_str ? I have the snippet

let base: u32 = from_str::<u32>(&var)

and get the error

Error: unresolved name from_str

So I googled this error and found this, so I tried adding the following

use std::u32;
use std::from_str::FromStr

But now I get

Error: unresolved import `std::from_str::FromStr` could not from `from_str` in `std` 

According to this github issue, I need to include this yet rust can't find it. What's going on? Here's a super simple program which gives me those errors

use std::u32;
use std::from_str::FromStr;

fn main() {

    let my_str = "String".to_string();
    let base: u32 = from_str(&my_str);
}

This is such a trivial issue and yet these resources are only showing me how to get more errors.

Upvotes: 4

Views: 1761

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90742

Use the docs to search for from_str. They show that the FromStr trait is in std::str now, not std::from_str. (April 2014 is a long time ago in Rust terms, being well before Rust 1.0.0.)

FromStr should not be used directly in general anyway; you should use str.parse method instead: my_str.parse::<u32>(). Remember that it returns a Result, because the string might just not contain a number.

Upvotes: 9

Related Questions