How can I forbid in Rust using a type alias in a function, enabling only the exact type? - Stack Overflow

admin2025-04-08  0

I have two ID types, both are Uuid:

type PersonId = Uuid;
type RoomId = Uuid;

And I have a function:

fn get_age(person: PersonId) -> u16 {
    ...
}

But I can call it with RoomId or even with Uuid. How can I restrict it to be callable with only exact PersonId type?

Edit: newtype pattern is a good solution, but it looks me somewhat overhead

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744124053a232379.html

最新回复(0)