Cannot infer type for type parameter `T` declared on the trait `Into`

Hello,

Im now trying to solve this rustling

// test2.rs

// This is a test for the following sections:

// - Strings

// Ok, here are a bunch of values-- some are `Strings`, some are `&strs`. Your

// task is to call one of these two functions on each value depending on what

// you think each value is. That is, add either `string_slice` or `string`

// before the parentheses on each line. If you're right, it will compile!

// I AM NOT DONE

fn string_slice(arg: &str) {

    println!("{}", arg);

}

fn string(arg: String) {

    println!("{}", arg);

}

fn main() {

    ("blue");

    ("red".to_string());

    (String::from("hi"));

    ("rust is fun!".to_owned());

    ("nice weather".into());

    (format!("Interpolation {}", "Station"));

    (&String::from("abc")[0..1]);

    ("  hello there ".trim());

    ("Happy Monday!".to_string().replace("Mon", "Tues"));

    ("mY sHiFt KeY iS sTiCkY".to_lowercase());

}

How can I solve this one.
I think it's now a &str but needs to be a String.
and the error is on this line : ("nice weather".into());

into() is for generic conversions, and needs to know what kind of value it should generate. Usually, the compiler can figure that out from context, but here you’re not doing anything with the result value. That means any type could be right, and the compiler doesn’t know what you actually want.

If you do something with the value that requires a particular type, like &str or String, the compiler will be able to figure out which type to convert to and won’t give you this error. (It might give you a different one if it doesn’t know how to do the conversion you asked for).

In this case, either function string_slice("nice weather".into()); or string("nice weather".into()); should give the compiler enough context to move forward.

Thanks

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.