November 2017
Intermediate to advanced
264 pages
5h 45m
English
In Chapter 6, Using Traits and OOP in Rust, in the Traits section, we developed in traits.rs structs for Alien, Zombie, and Predator characters that implemented a trait, Monster. The code file contained a main() function to make it executable. We will now incorporate this code (without the main() part) in a library project called monsters, and see how we can call this code.
Create the library project with cargo new monsters, creating a folder structure and a monsters/src/lib.rs file with template code:
#[test]
fn it_works() {
}
Remove this code and replace it with the code from Chapter 6/code/traits.rs, but omit the main() function. We also add a simple print_from_monsters() function to test calling it from the ...
Read now
Unlock full access