File: private_fields.rs

package info (click to toggle)
rust-derive-builder 0.20.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 500 kB
  • sloc: makefile: 2
file content (26 lines) | stat: -rw-r--r-- 692 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#[macro_use]
extern crate derive_builder;

/// This builder is in an inner module to make sure private fields aren't accessible
/// from the `main` function.
mod inner {
    /// The `LoremBuilder` struct will have private fields for `ipsum` and `dolor`, and
    /// a public `sit` field.
    #[derive(Debug, Builder)]
    #[builder(field(private), setter(into))]
    pub struct Lorem {
        ipsum: String,
        dolor: u16,
        #[builder(field(public))]
        sit: bool,
    }
}

fn main() {
    use inner::LoremBuilder;
    
    let mut lorem = LoremBuilder::default();
    lorem.dolor(15u16);
    lorem.sit = Some(true); // <-- public
    lorem.dolor = Some(0); // <-- private
}