-
In the bin folder, create a file called conversion.rs.
-
Add the following code and run it with cargo run --bin conversion:
1 use std::ops::MulAssign;
2 use std::fmt::Display;
3
4 // This structure doubles all elements it stores
5 #[derive(Debug)]
6 struct DoubleVec<T>(Vec<T>);
7
8
9 // Allowing conversion from a Vec<T>,
10 // where T is multipliable with an integer
11 impl<T> From<Vec<T>> for DoubleVec<T>
12 where
13 T: MulAssign<i32>,
14 {
15 fn from(mut vec: Vec<T>) -> Self {
16 for elem in &mut vec {
17 *elem *= 2;
18 }
19 DoubleVec(vec)
20 }
21 }
22
23 // Allowing conversion from a slice of Ts
24 // where T is again multipliable with an integer
25 impl<'a, T> From<&'a [T]> for DoubleVec<T>
26 where
27 T: MulAssign<i32> + Clone,
28 {
29 fn from(slice: &[T]) -> Self {
30 // Vec<T: MulAssign...