Extracting value from Option with match gives "unable to infer enough type information..." error

Christopher Reid

I'm trying simply to convert an inputted string into an integer. I know this can be done by applying unwrap to from_str but I figured I'd try to handle the error case properly and extract the value with a match instead. Instead of it working as expected I just get this error...

error: unable to infer enough type information about _; type annotations requiredwhich points to thexinSome(x)`

fn main () {
    let ref num = os::args()[1];
    match from_str(num.as_slice()) {
        Some(x) => println!("{}", x),
        None => println!("string cannot be converted to int"),
    }
}

What am I missing?

Chris Morgan

Let’s look at the definition of from_str:

pub fn from_str<A: FromStr>(s: &str) -> Option<A>

The return type, you see, may be an Option of any type that implements FromStr, such as int, u8 and IpAddr. If you don’t say explicitly what A is, by changing from_str(…) to from_str::<int>(…) or similar, the compiler will try to figure out what it can be. In this case, it will look forward and see that x is of type A and based on the usage of x, it must implement std::fmt::Show as well. There is nothing else that it can infer.

Well then, what types implement both FromStr and Show? There are quite a few of them, and as soon as there’s more than one it is impossible for the compiler to decide what you meant, so it gives up.

The solution is to stipulate what type you are wishing to parse that argument as, by writing from_str::<int>(num.as_slice()) or similar.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

error: unable to infer enough type information about `_`; type annotations required

From Dev

Rust error: unable to infer enough type information to locate the impl of the trait

From Dev

Error: unable to infer enough type information about `_`; type annotations or generic parameter binding required

From Dev

Unable to infer enough type information about _; type annotations or generic parameter binding required

From Dev

Unable to infer enough type information about `_`; type annotations or generic parameter binding required

From Dev

Filtering a list of (A, Option[B]) and extracting value from Option

From Dev

How to specify default value when extracting Option from Option case class

From Dev

Fetching session value from webmethod gives error

From Dev

extracting evidence of equality from match

From Dev

Extracting a value from an union

From Dev

Extracting a value from an integer

From Dev

Extracting a value from an integer

From Dev

Unable to use --suites option in protractor, gives 'Spec patterns did not match any files' error

From Dev

Overloading * operator gives no match error

From Dev

decimal value gives error

From Dev

Extracting specific option price data from Excels

From Dev

Selecting value from dropdownlist gives error in asp.net

From Dev

nvcc -arch sm_52 gives error "Value 'sm_52' is not defined for option 'gpu-architecture'"

From Dev

nvcc -arch sm_52 gives error "Value 'sm_52' is not defined for option 'gpu-architecture'"

From Dev

extracting value from xml node

From Dev

extracting wrong value from database

From Dev

Extracting value from XMLType Column

From Dev

Swift: Extracting value from optional

From Dev

Extracting value from nested dictionary

From Dev

Bash extracting value from pattern

From Dev

Extracting specific value from a file

From Dev

Extracting value from Posted JSON

From Dev

extracting value from IOPSCopyPowerSourcesInfo() swift

From Dev

Extracting words from lines that match different patterns

Related Related

  1. 1

    error: unable to infer enough type information about `_`; type annotations required

  2. 2

    Rust error: unable to infer enough type information to locate the impl of the trait

  3. 3

    Error: unable to infer enough type information about `_`; type annotations or generic parameter binding required

  4. 4

    Unable to infer enough type information about _; type annotations or generic parameter binding required

  5. 5

    Unable to infer enough type information about `_`; type annotations or generic parameter binding required

  6. 6

    Filtering a list of (A, Option[B]) and extracting value from Option

  7. 7

    How to specify default value when extracting Option from Option case class

  8. 8

    Fetching session value from webmethod gives error

  9. 9

    extracting evidence of equality from match

  10. 10

    Extracting a value from an union

  11. 11

    Extracting a value from an integer

  12. 12

    Extracting a value from an integer

  13. 13

    Unable to use --suites option in protractor, gives 'Spec patterns did not match any files' error

  14. 14

    Overloading * operator gives no match error

  15. 15

    decimal value gives error

  16. 16

    Extracting specific option price data from Excels

  17. 17

    Selecting value from dropdownlist gives error in asp.net

  18. 18

    nvcc -arch sm_52 gives error "Value 'sm_52' is not defined for option 'gpu-architecture'"

  19. 19

    nvcc -arch sm_52 gives error "Value 'sm_52' is not defined for option 'gpu-architecture'"

  20. 20

    extracting value from xml node

  21. 21

    extracting wrong value from database

  22. 22

    Extracting value from XMLType Column

  23. 23

    Swift: Extracting value from optional

  24. 24

    Extracting value from nested dictionary

  25. 25

    Bash extracting value from pattern

  26. 26

    Extracting specific value from a file

  27. 27

    Extracting value from Posted JSON

  28. 28

    extracting value from IOPSCopyPowerSourcesInfo() swift

  29. 29

    Extracting words from lines that match different patterns

HotTag

Archive