- Struct types
- Tuple Structs
- Methods and their use
- Private and public data, access rules
#[derive(Debug)]
struct S {
id : u64,
name : String,
}
fn main() {
let s : S = S{
id : 12,
name : "Mustermann".to_string(),
};
println!("{:?} {} {}", s, s.id, s.name);
}
#[derive(Debug)]
struct S(u64, String);
#[derive(Debug)]
struct U;
fn main() {
let s : S = S(12, "Mustermann".to_string());
let u : U = U;
println!("{:?} {} {} {:?}", s, s.0, s.1, u);
}
#[derive(Debug)]
struct S {
id : u64,
name : String,
}
impl S {
fn new(c : u64, s : &str) -> S {
S{id: c, name: s.to_string()}
}
fn show(&self) {
println!("S: id={} name={}", self.id, self.name);
}
fn inc(&mut self) {
self.id += 1;
}
}
fn main() {
let s : S = S{
id : 12,
name : "Mustermann".to_string(),
};
let mut s2 = S::new(1u64, "Mueller");
s2.inc();
s2.show();
println!("{:?} {} {} {:?}", s, s.id, s.name, s2);
}
mod m {
#[derive(Debug)]
pub struct S {
pub id : u64,
name : String,
}
impl S {
pub fn new(c : u64, s : &str) -> S {
S{id: c, name: s.to_string()}
}
pub fn show(&self) {
println!("S: id={} name={}", self.id, self.name);
}
pub fn inc(&mut self) {
self.id += 1;
}
}
}
fn main() {
let mut s = m::S::new(1u64, "Mueller");
s.inc();
s.show();
println!("{:?} {}", s, s.id);
}