summaryrefslogblamecommitdiff
path: root/meap/meap-code/ch3/ch3-file-doced.rs
blob: 18e35f01f015b89a8e7806cfe95a12567ddfb7ca (plain) (tree)





































                                                                 
//! 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);
}