#derive #getter #boilerplate #getters

macro derive-getters

Simple boilerplate getters generator

7 releases

0.0.7 Jun 24, 2018
0.0.6 Jun 24, 2018
0.0.4 Apr 28, 2018
0.0.3 Oct 8, 2017
0.0.2 Apr 23, 2017
Download history 776/week @ 2018-12-20 1039/week @ 2018-12-27 21/week @ 2019-01-03 46/week @ 2019-01-10 19/week @ 2019-01-17 8/week @ 2019-01-24 48/week @ 2019-01-31 41/week @ 2019-02-07 36/week @ 2019-02-14 34/week @ 2019-02-21 23/week @ 2019-02-28 24/week @ 2019-03-07 77/week @ 2019-03-14 275/week @ 2019-03-21 126/week @ 2019-03-28

975 downloads per month
Used in 2 crates (1 directly)

MIT license

75 lines

Derive Getters

Simple boilerplate library for getters.

The need for this library came about when I was making various data structures for JSON to deserialize into. These data structures had many fields in them and weren't going to change once created. Of course one could just use pub everywhere.

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


Add to your Cargo.toml:

derive-getters = "0.0.7"

Then put this in your rust project root.

extern crate derive_getters;


#[macro_use] extern crate derive_getters;

PS. This way is better. It makes the code run faster.


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.

impl MyCheesyStruct {
    pub fn get_x(&self) -> i64 {

    pub fn get_y(&self) -> i64 {


This crate will not yet create getters for unit structs, tuples or enums. If you try to derive over them, the code will just ignore you and do nothing. (Please let me know by filing an issue if this isn't the right way to handle things. Should this crate panic if attempting to derive on an unsupported value?)

Getters for String fields should return a &str. Right now you have to do an awkward &my_struct.name()[..] to shoehorn it into some semblance of a &str.


~27K SLoC