mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-10-31 21:48:49 +00:00
51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
// ./src/mod/struct_visibility.md
|
|
|
|
|
|
mod my {
|
|
// A public struct with a public field of generic type `T`
|
|
pub struct OpenBox<T> {
|
|
pub contents: T,
|
|
}
|
|
|
|
// A public struct with a private field of generic type `T`
|
|
#[allow(dead_code)]
|
|
pub struct ClosedBox<T> {
|
|
contents: T,
|
|
}
|
|
|
|
impl<T> ClosedBox<T> {
|
|
// A public constructor method
|
|
pub fn new(contents: T) -> ClosedBox<T> {
|
|
ClosedBox {
|
|
contents: contents,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn part0() {
|
|
// Public structs with public fields can be constructed as usual
|
|
let open_box = my::OpenBox { contents: "public information" };
|
|
|
|
// and their fields can be normally accessed.
|
|
println!("The open box contains: {}", open_box.contents);
|
|
|
|
// Public structs with private fields cannot be constructed using field names.
|
|
// Error! `ClosedBox` has private fields
|
|
//let closed_box = my::ClosedBox { contents: "classified information" };
|
|
// TODO ^ Try uncommenting this line
|
|
|
|
// However, structs with private fields can be created using
|
|
// public constructors
|
|
let _closed_box = my::ClosedBox::new("classified information");
|
|
|
|
// and the private fields of a public struct cannot be accessed.
|
|
// Error! The `contents` field is private
|
|
//println!("The closed box contains: {}", _closed_box.contents);
|
|
// TODO ^ Try uncommenting this line
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|