// Make the code compile by defining Operation enum with a single generic type.
fn main() {
let _op1 = Operation::Add(15u8, 10u8);
let _op2 = Operation::Mul(150, 23);
let _op3 = Operation::Sub {
left: 120,
right: 50,
};
let _op4 = Operation::Div {
dividend: 10.23,
divisor: 2.43,
};
}
Solution
enum Operation<T> {
Add(T, T),
Mul(T, T),
Sub { left: T, right: T },
Div { dividend: T, divisor: T },
}
fn main() {
let _op1 = Operation::Add(15u8, 10u8);
let _op2 = Operation::Mul(150, 23);
let _op3 = Operation::Sub {
left: 120,
right: 50,
};
let _op4 = Operation::Div {
dividend: 10.23,
divisor: 2.43,
};
}
// Fix the code so that it compiles.
fn take_and_give_ownership(input: T) -> {
input
}
struct User {
name: String,
id: u32,
}
fn main() {
let str1 = String::from("Ferris the 🦀!");
let user1 = User {
name: "Alice".to_string(),
id: 199,
};
let _str2 = take_and_give_ownership(str1);
let _user2 = take_and_give_ownership(user1);
}
Solution
fn take_and_give_ownership<T>(input: T) -> T {
input
}
struct User {
name: String,
id: u32,
}
fn main() {
let str1 = String::from("Ferris the 🦀!");
let user1 = User {
name: "Alice".to_string(),
id: 199,
};
let _str2 = take_and_give_ownership(str1);
let _user2 = take_and_give_ownership(user1);
}