I have a function with the following signature
pub fn get<'de, T>(key: &'de str) -> Result<T>
where
T: serde::Deserialize<'de>,
The returned type can be anything that implements serde::Deserialize. I can do something like this.
let graphql_url: String = AppConfig::get("graphql.url").unwrap();
This code works correctly. It gets the value of "graphql.url" as a "String". The type coercion seems to work. I can use the String as a &str with another function. This function is graphiql_source (juniper::http::graphiql::graphiql_source - Rust) which takes a &str.
graphiql_source(&graphql_url); // -> this code compiles and runs.
So where is the problem? The problem is when I try to do this.
graphiql_source(AppConfig::get("graphql.url").unwrap());
My understanding is that Rust will need to coerce "AppConfig::get("graphql.url").unwrap()" into a &str. This code compiles but as soon as it gets executed, I get the following error
inner: invalid type: string "http://127.0.0.1:4556", expected a borrowed string
So the type of "AppConfig::get("graphql.url").unwrap()" is a String. But the function accepts a &str. How come this code is compiling in the first place?