Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct Index {
- inner: Vec<Description>
- }
- impl Index {
- fn insert(&mut self, element: Description) {
- self.inner.push(element);
- }
- }
- #[derive(Debug)]
- struct Description {
- foo: String,
- bar: String,
- }
- impl<'a, T> From<&'a [T]> for Index
- where &'a T: Into<Description>
- {
- fn from(vec: &'a [T]) -> Index {
- let mut index = Index { inner: Vec::new() };
- for element in vec {
- index.insert(element.into());
- }
- index
- }
- }
- struct Other(String, String);
- impl Other {
- fn new(foo: &str, bar: &str) -> Self {
- Other(foo.to_owned(), bar.to_owned())
- }
- }
- impl From<&Other> for Description {
- fn from(other: &Other) -> Description {
- Description {
- foo: other.0.clone(),
- bar: other.1.clone(),
- }
- }
- }
- fn main() {
- let others = vec![
- Other::new("a", "b"),
- Other::new("c", "d"),
- ];
- let index = Index::from(others.as_slice());
- println!("{:#?}", index);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement