I have a background in typescript and recently started learning Rust. I am wondering how I would approach something like the following in Rust – or: what's the Rusty way?
// base type, used widely within the application
type User = {
id: UUID
name: string
email: string
}
// -- specialized types --
// might be used only for the API route
// e.g. a POST endpoint where some properties are omitted
type PostUser = Omit<User, "id">
// explanation: the Omit utility type creates a new type `PostUser` based on `user` but removes the property `id`
// or this one where the database holds more properties that are excluded when deserializing
type DBUser = User & { deleted: boolean }
// explanation: this also creates a new type but adds another property `deleted`
This is not possible in Rust and there are no similar techniques, I guess. But how do you guys keep code maintainable?
Do I really have to copy the whole struct? It would be pain in the ass when it comes to refactoring. Or does Rust offer different techniques I am not aware of?
struct User {
pub id: Uuid,
pub name: String,
}
// copying might be simple here, but maintaining multiple properties across hundred files?
struct PostUser {
// pub id: Uuid, // <- The POST endpoint does not receive an id, it's set afterwards
pub name: String,
}
Thanks for clarifying this for me!