November 2017
Intermediate to advanced
264 pages
5h 45m
English
Let's see another example of how to work with external crates using the rand crate.
Games often involve random choices, such as deciding which monster should attack our player or at which place you will find treasure; the rand ;crate provides for this. Start a Cargo project, random. To compile the rand crate with your program, add the following to the Cargo.toml configuration file:
[dependencies] rand = "*"
To import the crate into your program, add the following lines at the start of your source code, random\scr\main.rs:
extern crate rand; use rand::Rng;
The rand crate contains a random() function for generating a random value, but it can generate values of lots of different types, not only integer numbers; ...
Read now
Unlock full access