msnoyman: I'm at a loss to figure out how to use a method in the implementation of the Fruit<i32> and use it to return a Fruit<String> [if I'm on the right track]. It seems you want something like:
struct Fruit<T> {
apples: T,
bananas: T,
}
impl Fruit<i32> {
fn new() -> Self {
Fruit {
apples: 5,
bananas: 10,
}
}
fn stringy() -> Fruit<String>{
Fruit {
apples: "5".to_string(),
bananas: "10".to_string(),
}
}
}
fn main() {
let fruit_i32: Fruit<i32> = Fruit::new();
let fruit_str: Fruit<String> = fruit_i32.stringy();
assert_eq!(fruit_str.apples, "5".to_owned());
assert_eq!(fruit_str.bananas, "10".to_owned());
println!("Success!");
}
But of course, this doesn't compile and hence, doesn't work. But this does [I gotta go back and read the 'self as the first implied parameter section'
struct Fruit<T> {
apples: T,
bananas: T,
}
impl Fruit<i32> {
fn new() -> Self {
Fruit {
apples: 5,
bananas: 10,
}
}
fn stringy(self) -> Fruit<String>{
Fruit {
apples: "5".to_string(),
bananas: "10".to_string(),
}
}
}
fn main() {
let fruit_i32: Fruit<i32> = Fruit::new();
let fruit_str: Fruit<String> = fruit_i32.stringy();
assert_eq!(fruit_str.apples, "5".to_owned());
assert_eq!(fruit_str.bananas, "10".to_owned());
println!("Success!");
}