summaryrefslogtreecommitdiff
path: root/meap/meap-code/ch3/ch3-public-file.rs
blob: ac6a9f54d28f86be98db015c3e2b8dd40d2e164e (plain) (blame)
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
27
28
29
30
31
32
33
#[derive(Debug)]
pub struct File { // <1>
  name: String,   // <2>
  data: Vec<u8>,  // <2>
}

impl File {
  pub fn new(name: &str) -> File {
    File {
      name: String::from(name),
      data: Vec::new(),
    }
  }
  
  pub fn len(&self) -> usize {
    self.data.len()
  }
  
  pub fn name(&self) -> String {
    self.name.clone() // <3>
  }
}


fn main() {
  let f1 = File::new("f1.txt");
  
  let f1_name = f1.name(); // <4>
  let f1_length = f1.len();
  
  println!("{:?}", f1);
  println!("{} is {} bytes long", f1_name, f1_length);
}