Inline type defination


#1

Hi,
Is possible single line type definition in rust ?
for example:
my return type is haven’t support &str but my next method needs str, so i must declare this like

let res: String = id;
uuid::Uuid::parse_str(&res).unwrap().simple().to_string()

Is available any solution to removing first line ?
something like this ?
uuid::Uuid::parse_str(&(res as String)).unwrap().simple().to_string()


#2

You don’t need the first line. The following should be fine:

uuid::Uuid::parse_str(&id).unwrap().simple().to_string()

#3

Origin (id) not have &str trait but it’s have String, and destination just support &str.
if i use your code i will this error:

cannot infer type for


#4

What is the type of id? I assumed it was String, but it sounds like that’s not the case. Please provide a minimal example that shows your problem, if you can, ideally on the playground.


#5

Unfortunately playground could not support rusqlite library.
Id is a trait result. https://docs.rs/rusqlite/0.13.0/rusqlite/types/trait.FromSql.html

Sorry when i uses your code, kind of error was different of cannot infer type for
I did attached my problem SC.


#6

Try the following on line 65:

|r| r.get::<_, String>(0)

with all else the same.


#7

Thanks man :bouquet::bouquet:


#8

I want read more about ::<_, String> what’s name of this section in rust documents ?


#9

The term to search would be “turbofish” - it’s the way to explicitly state the type you want, when type inference cannot determine it (like in the case here). I now see why you had let res: String = id - that informed type inference to use String as well.