Cannot infer types

i tried implementing functional programming style in a function that parses a string type vector into i64

here is the code :

    pub fn vector_conver_int(arg: Vec<String>) -> Result<Vec<String>,Error> {	
	let mut integer_vector :Vec<i64> = Vec::with_capacity(1000);
	Ok(
		arg.iter().flat_map(|data| data.trim().parse().expect("failure in conversion").collect())
   	)

}

here is the error :

> error[E0282]: type annotations needed
>   --> src\fxx.rs:96:31
>    |
> 96 |             arg.iter().flat_map(|data| data.trim().parse().expect("failure in conversion").collect())
>    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `T`
>    |
>    = note: type must be known at this point

I changed the return type, flat_map to map and changed it to parse::<i64>()

pub fn vector_conver_int(arg: Vec<String>) -> Result<Vec<i64>, Error> {
    Ok(arg
        .iter()
        .map(|data| data.trim().parse::<i64>().expect("failure in conversion"))
        .collect())
}
1 Like

whats the difference between map and flat_map? can you link me to docs of flat_map

sure
map vs. flat_map

1 Like

thanks mate

dude , here is a little compile error :
error[E0599]: no method named collect found for type i64 in the current scope
--> src\fxx.rs:96:85
|
96 | arg.iter().map(|data| data.trim().parse::().expect("failure in conversion").collect())
| ^^^^^^^
|
= note: the method collect exists but the following trait bounds were not satisfied:
&mut i64 : std::iter::Iterator

error[E0308]: mismatched types
  --> src\fxx.rs:96:4
   |
96 |             arg.iter().map(|data| data.trim().parse::<i64>().expect("failure in conversion").collect())
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::vec::Vec`, found struct `std::iter::Map`
   |
   = note: expected type `std::vec::Vec<std::string::String>`
              found type `std::iter::Map<std::slice::Iter<'_, std::string::String>, _>`

error: aborting due to 2 previous errors

my bad , i got it working