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-1.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 meap/meap-code/ch3/ch3-not-quite-file-1.rs (limited to 'meap/meap-code/ch3/ch3-not-quite-file-1.rs') diff --git a/meap/meap-code/ch3/ch3-not-quite-file-1.rs b/meap/meap-code/ch3/ch3-not-quite-file-1.rs new file mode 100644 index 0000000..292d7ab --- /dev/null +++ b/meap/meap-code/ch3/ch3-not-quite-file-1.rs @@ -0,0 +1,23 @@ +#![allow(unused_variables)] // <1> Relax compiler warnings while working through ideas + +type File = String; // <2> Create a type alias. The compiler won't distinguish between String & File, but your source code will. + +fn open(f: &mut File) -> bool { + true // <3> let's assume for the moment that this always succeeds +} + +fn close(f: &mut File) -> bool { + true // <3> +} + +#[allow(dead_code)] // <4> Relaxes a compiler warning about an unused function +fn read(f: &mut File, save_to: &mut Vec) -> ! { // <5> Using `!` as a return type indicates to the Rust compiler that this function never returns + unimplemented!() // <6> A macro that crashes the program if it is encountered +} + +fn main() { + let mut f1 = File::from("f1.txt"); // <7> With the type declaration at line 3, `File` "inherits" all of String's methods + open(&mut f1); + //read(f1 , vec![]); // <8> There's little point in calling this method + close(&mut f1); +} \ No newline at end of file -- cgit v1.2.3