13 releases

0.4.0 May 12, 2024
0.3.0 May 25, 2023
0.2.1 Apr 19, 2023
0.2.0 Aug 18, 2020
0.0.2 Apr 23, 2017

#31 in Procedural macros

Download history 25639/week @ 2024-01-29 25614/week @ 2024-02-05 29264/week @ 2024-02-12 26579/week @ 2024-02-19 28321/week @ 2024-02-26 28494/week @ 2024-03-04 29442/week @ 2024-03-11 28882/week @ 2024-03-18 28437/week @ 2024-03-25 31445/week @ 2024-04-01 36782/week @ 2024-04-08 37899/week @ 2024-04-15 37496/week @ 2024-04-22 42492/week @ 2024-04-29 47508/week @ 2024-05-06 42474/week @ 2024-05-13

171,745 downloads per month
Used in 119 crates (60 directly)

MIT license

526 lines

Derive Getters

Simple Getters derive macro for generating field getter methods on a named struct. Included is an additional derive, Dissolve, that consumes the struct returning a tuple of all fields in the order they were declared. Dissolve can also be derived for tuple structs. Any doc comments on the target struct fields are replicated for the corresponding getters. If no comment is present one shall be generated.

The need for the Getters macro came about when I was making various data structures for JSON to deserialize into. These data structures had many fields in them to access and they weren't going to change once created. One could use pub everywhere but that would enable mutating the fields which is what this derive aims to avoid.

Getters will be generated according to convention. This means that the generated methods will reside within the struct namespace.

With regards to Dissolve, sometimes during conversion a structure must be consumed. One easy way to do this is to return a tuple of all the structs fields. Thus Dissolve can be considered a 'get (move) everything' method call.

What this crate won't do

There are no mutable getters and it's not planned. There are no setters either nor will there ever be.

Rust Docs

Documentation is here.


Add to your Cargo.toml:

derive-getters = "0.4.0"

Then import the Getters or Dissolve macro in whichever module it's needed (assuming 2018 edition).

use derive_getters::{Getters, Dissolve};

Otherwise just import at crate root.

extern crate derive_getters;


When you have a struct you want to automatically derive getters for... Just add the derive at the top like so;

pub struct MyCheesyStruct {
    x: i64,
    y: i64,

A new impl will be produced for MyCheesyStruct.

/// Auto-generated by `derive_getters::Getters`.
impl MyCheesyStruct {
    /// Get field `x` from instance of `MyCheesyStruct`.
    pub fn x(&self) -> &i64 {

    /// Get field `y` from instance of `MyCheesyStruct`.
    pub fn y(&self) -> &i64 {

This crate can also handle structs with simple generic parameters and lifetime annotations. Check docs for further details.

pub struct StructWithGeneric<'a, T> {
    concrete: f64,
    generic: T,
    text: &'a str,

With Dissolve, use it like so;

pub struct Solid {
    a: u64,
    b: f64,
    c: i64,

An impl will be produced for Solid like so;

/// Auto-generated by `derive_getters::Dissolve`.
impl Solid {
    /// Dissolve `Solid` into a tuple consisting of its fields in order of declaration.
    pub fn dissolve(self) -> (u64, f64, i64) {
      (self.a, self.b, self.c)


This macro comes with two optional field attributes for Getters.

  • #[getter(skip)] to skip generating getters for a field.
  • #[getter(rename = "name")] to change the getter name to "name".

And one optional struct attribute for Dissolve.

  • #[dissolve(rename = "name")] to change the name of the dissolve function to "name".

Comment Preservation

Doc comments on fields shall be copied over onto their respective getters. If there is no comment for the field then one shall be generated.

For example, this struct;

#[derive(Getters, Dissolve)]
#[dissolve(rename = "melt")]
struct Funky<'a, T> {
    number: i64,

    /// This is commented.
    commented: T,

    /// This comment is over
    /// many lines.
    /// Here's another line.
    #[getter(rename = "floating")]
    many_line: f32,

     * This is one of those
     * pesky multi-line
     * comments.
    multi_line: &'a str,

    /// We don't care for this field.
    ignore_me: u16,

    #[doc = r" A doc comment"]
    one_comment: u64,

    #[doc = include_str!("../COMMENTS.md")]
    incl_comment: u64,

    /// This comment should be hidden.
    hideme: u64,

    // This comment won't show up.
    bad: u64,

Shall have this auto-generated for it;

/// Auto-generated by `derive_getters::Getters`.
impl<'a, T> Funky<'a, T> {
    /// Get field `number` from instance of `Funky`.
    pub fn number(&self) -> &i64 {
    /// This is commented.
    pub fn commented(&self) -> &T {
    /// This comment is over
    /// many lines.
    /// Here's another line.
    pub fn floating(&self) -> &f32 {
     * This is one of those
     * pesky multi-line
     * comments.
    pub fn multi_line(&'a self) -> &'a str {
    /// A doc comment
    pub fn one_comment(&self) -> &u64 {
    /**Comments sourced from a file included at compile time.
    pub fn incl_comment(&self) -> &u64 {
    /// This comment should be hidden.
    pub fn hideme(&self) -> &u64 {
    /// Get field `bad` from instance of `Funky`.
    pub fn bad(&self) -> &u64 {
/// Auto-generated by `derive_getters::Dissolve`.
impl<'a, T> Funky<'a, T> {
    /// Dissolve `Funky` into a tuple consisting of its fields in order of declaration.
    pub fn melt(self) -> (i64, T, f32, &'a str, u16, u64, u64, u64, u64) {

See? Now with comments!


  1. Getters cannot be derived for unit structs, tuple structs, or enums.
  2. Dissolve cannot be derived for unit structs or enums.
  3. All getter methods return an immutable reference, &, to their field. This means for some types it can get awkward.




~18K SLoC