I'd like to use the From trait to extract values from a json input into my struct CreateUser. If I extract the full etc vector as etc: Vec<String> it works fine, but I only need the second field of etc which is 2 in the CreateUser struct. Following the compiler hints I also couldn't get it to work.
The code:
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
static INPUT: &str = r#"{
"email" : "",
"password" : "",
"etc" : ["1", "2"]
}"#;
#[derive(Serialize, Deserialize, Debug)]
struct InputUser {
email: String,
password: String,
etc: Vec<String>,
}
struct CreateUser<'a> {
email: String,
password: String,
only_second_field_in_etc_vector: &'a str,
}
impl From<InputUser> for CreateUser {
fn from(i: InputUser) -> CreateUser {
CreateUser {
email: i.email,
password: i.password,
// Problem here
only_second_field_in_etc_vector: &i.etc[1],
}
}
}
fn main() {
let input: InputUser = serde_json::from_str(INPUT).unwrap();
let create_user = CreateUser::from(input);
}