From 67cdcc2e12118becb823e20a40cc2687f2b8425a Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Wed, 27 Mar 2019 15:32:37 -0400 Subject: Started Rust in Action MEAP. --- meap/meap-code/ch3/ch3-not-quite-file-3.rs | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 meap/meap-code/ch3/ch3-not-quite-file-3.rs (limited to 'meap/meap-code/ch3/ch3-not-quite-file-3.rs') diff --git a/meap/meap-code/ch3/ch3-not-quite-file-3.rs b/meap/meap-code/ch3/ch3-not-quite-file-3.rs new file mode 100644 index 0000000..a88d112 --- /dev/null +++ b/meap/meap-code/ch3/ch3-not-quite-file-3.rs @@ -0,0 +1,55 @@ +#![allow(unused_variables)] + +#[derive(Debug)] +struct File { + name: String, + data: Vec, +} + +impl File { + fn new(name: &str) -> File { + File { + name: String::from(name), + data: Vec::new(), + } + } + + fn new_with_data(name: &str, data: &Vec) -> File { // <1> This method has snuck in to deal with cases where we want to simulate cases where a file has pre-existing data + let mut f = File::new(name); + f.data = data.clone(); + f + } + + fn read(self: &File, save_to: &mut Vec) -> usize { // <2> The `f` argument has been replaced with `self` + let mut tmp = self.data.clone(); + let read_length = tmp.len(); + save_to.reserve(read_length); + save_to.append(&mut tmp); + read_length + } +} + +fn open(f: &mut File) -> bool { // <3> These two can remain as-in until we've looked at error handling + true +} + +fn close(f: &mut File) -> bool { // <3> + true +} + +fn main() { + let f3_data: Vec = vec![114, 117, 115, 116, 33]; // <4> An explicit type needs to be provided, as `vec!` can't infer the necessary type through the function boundary + let mut f3 = File::new_with_data("2.txt", &f3_data); + + let mut buffer: Vec = vec![]; + + open(&mut f3); + let f3_length = f3.read(&mut buffer); // <5> Here is the the change in the calling code + close(&mut f3); + + let text = String::from_utf8_lossy(&buffer); + + println!("{:?}", f3); + println!("{} is {} bytes long", &f3.name, f3_length); + println!("{}", text); +} \ No newline at end of file -- cgit v1.2.3