Parameterized Functions
We'll cover the following...
I want to say hi to Alice and Bob. But for now, at least, I’m going to be polite and not mention how old they are. How exactly do we write that function? Let’s first write a version that works for Alice. This code won’t compile; try to see if you can figure out why.
Press + to interact
struct Person<Name, Age> {name: Name,age: Age,}fn greet(person: &Person<String, u32>) {println!("Hello, {}", person.name);}fn main() {let alice: Person<String, u32> = Person {name: "Alice".to_owned(),age: 30_u32,};greet(&alice);let bob: Person<String, u64> = Person {name: "Bob".to_owned(),age: 35_u64,};greet(&bob);}
The compiler gives us the following error message:
...