I'm still very new to Rust and have been trying small exercises to see how they differ from other languages I'm more familiar with and just get more understanding of the language.
I've been reading the ebook and doing my own version of the learn by example and got up to the Generics and Traits which has got me quite confused.
In C# if I start with object of type A and want to project it out into type B and type C, I'd make a generic interface with the required method and then a class to inherit from the interface and then implement the method in the class to accept A as a parameter and return B, but in Rust I've been having some issues getting my head around it.
I followed the same approach trying to translate what I understand from docs and examples and my code works but not 100% sure if this is the most correct solution?
Any tips or advice would be appreciated
fn main() {
println!("Hello, world!");
let test: C = C { prop_A: String::from("8"), prop_B: String::from("Test description") };
let a: A = C::convert(&test);
let b: B = C::convert(&test);
println!("{a:#?}");
println!("{b:#?}");
}
#[derive(Debug)]
pub struct A {
field_A: String,
field_B: String
}
#[derive(Debug)]
pub struct B {
prop_A: u8,
prop_B: String
}
pub struct C {
prop_A: String,
prop_B: String
}
trait Convert<T> {
fn convert(c: &C) -> T;
}
impl Convert<A> for C {
fn convert(c: &C) -> A {
A { field_A: c.prop_A.clone(), field_B: c.prop_B.clone() }
}
}
impl Convert<B> for C {
fn convert(c: &C) -> B {
B { prop_A: c.prop_A.clone().parse().unwrap(), prop_B: c.prop_B.clone() }
}
}