concept impl in category rust
appears as: impl

This is an excerpt from Manning's book Rust in Action.
We also discuss how to add methods to types with
implblocks. Lastly, we take a good look at traits, Rust’s system for defining interfaces.[1]
To enable these changes, you make use of an
implblock, shown below in Listing 3. 9. The resulting executable should print out the same message as Listing 3. 6, substituting "f3.txt" for the original’s "f1.txt".Listing 3. 9. Using
implto add methods to a struct (ch3-defining-files-neatly.rs)#[derive(Debug)] struct File { name: String, data: Vec<u8>, } impl File { fn new(name: &str) -> File { #1 File { #2 name: String::from(name), #2 data: Vec::new(), #2 } } } fn main() { let f3 = File::new("f3.txt"); let f3_name = &f3.name; #3 let f3_length = f3.data.len(); println!("{:?}", f3); println!("{} is {} bytes long", f3_name, f3_length); }
To define methods, Rust programmers use an
implblock that are physically distinct in source code from thestruct(andenum) blocks that you have already encountered.