Newtype (usize) efficacy and idiomatic usage
⚓ Rust 📅 2026-04-12 👤 surdeus 👁️ 7I have a newtype
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
// Type wrapper for Author IDs
pub struct AuthorId(usize);
It works. Using it produces good results. Fixing various compiler errros (very clear messages from the compiler), I wondered about one aspect.
In a lot of places, when I have an aid of type AuthorId, and am passing it around, I need to ssay aid.clone() to tell the compiler I want a copy of the value.
As I understand it, if I were just using a usize, the compiler would do this automatically.
I think I could get the compiler to do this automatically for AuthorId by deriving Copy for it?
If so, is that a good idea, or is it more maintainable to have the explicit clone() calls?
Thanks,
Joel
4 posts - 3 participants
🏷️ Rust_feed