Getters for Copy types

⚓ Rust    📅 2025-09-01    👤 surdeus    👁️ 2      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Getters for Copy types

We have a small discussion in our team how we should design our API around smallish non-trivial copy fields.

So lets say we have something like this:

#[derive(Copy)]
pub struct SmallishType {
     x: Duration,
     y: u32
}

pub struct Holder {
    z: SmallishType,
    ...
}

and we want now to implement a getter for Holder::z, should this getter return a reference or an owned value, i.e.

fn z(&self) -> &SmallishType { ... }

vs

fn z(&self) -> SmallishType { ... }

6 posts - 5 participants

Read full topic

🏷️ Rust_feed