summaryrefslogtreecommitdiff
path: root/meap/meap-code/ch3/ch3-file-doced.rs
blob: 18e35f01f015b89a8e7806cfe95a12567ddfb7ca (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
34
35
36
37
38
//! Simulating files one step at a time.

/// Represents a "file", which probably lives on a file system.
#[derive(Debug)]
pub struct File {
  name: String,
  data: Vec<u8>,
}

impl File {
  /// New files are assumed to be empty, but a name is required.
  pub fn new(name: &str) -> File {
    File {
      name: String::from(name),
      data: Vec::new(),
    }
  }
  
  pub fn len(&self) -> usize {
    //! Returns the file's length in bytes.
    self.data.len()
  }
  
  pub fn name(&self) -> String {
    //! Returns the file's name.
    self.name.clone()
  }
}

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