fn main() {
#[derive(Debug, Clone, Hash, Default, PartialEq, Eq)]
pub struct Meta {
pub title: Option<String>,
pub passwords: Vec<String>,
pub tags: Vec<String>,
pub category: Option<String>,
}
impl Meta {
pub fn new<S: Into<String>>(title: Option<S>, passwords: Vec<S>, tags: Vec<S>, category: Option<S>) -> Self {
Self {
title: match title {
Some(text) => Some(text.into()),
None => None,
},
passwords: passwords.iter().map(|s: &S| -> String {s.into()}).collect(),
tags: passwords.iter().map(|s: &S| -> String {s.into()}).collect(),
category: match category {
Some(text) => Some(text.into()),
None => None,
},
}
}
}
let meta = Meta::new(Some("hello"), vec!["hello", "world"], vec!["hello", "world"], Some("world"));
println!("{:?}", meta)
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `String: From<&S>` is not satisfied
--> src/main.rs:17:70
|
17 | passwords: passwords.iter().map(|s: &S| -> String {s.into()}).collect(),
| ^^^^ the trait `From<&S>` is not implemented for `String`
|
= note: required for `&S` to implement `Into<String>`
help: consider dereferencing here
|
17 | passwords: passwords.iter().map(|s: &S| -> String {(*s).into()}).collect(),
| ++ +
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
10 | impl Meta where String: From<&S> {
| ++++++++++++++++++++++
error[E0277]: the trait bound `String: From<&S>` is not satisfied
--> src/main.rs:18:65
|
18 | tags: passwords.iter().map(|s: &S| -> String {s.into()}).collect(),
| ^^^^ the trait `From<&S>` is not implemented for `String`
|
= note: required for `&S` to implement `Into<String>`
help: consider dereferencing here
|
18 | tags: passwords.iter().map(|s: &S| -> String {(*s).into()}).collect(),
| ++ +
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
10 | impl Meta where String: From<&S> {
| ++++++++++++++++++++++
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 2 previous errors
I'm trying to implement a wider new
constructor that can accept more types than just calling Meta { ... }
. I do see the errors but I still can't wrap my head around what's really wrong and how do I go about fixing it