Last used the stdout, this time we write a little more complicated game
rust standard library named std, the default will be introduced.
This time we will use a random number function, the random number and embarrassing that this stuff is not in the standard library, we have to rely on extra a library.
Many programs have their own programming modular library system, rust is no exception, but the library is called rust crate, is the figure in this case the meaning is pretty image.
The official website provides a rust crates.io used to manage the library, you can also submit your library up
This time we will use this library rand
Dependent libraries
Because the query to the current version of the library rand has come to 0.7.2
Then we rely on a good definition of 0.7.0.
Cargo.toml edit files, plus rely on just fine
0.7.0 written, in fact, will find their latest 0.7.x version of rand
Dependent libraries only need to edit the file to cargo.toml, then rust will build upon themselves to Download Library
The most interesting part of the rust and then came
View Document
Cargo doc -open only need to enter the command line
rust will automatically generate a local code documentation for you, of course, include dependent libraries
By local documents, we probably know how to use the rand
Then write code using this library, documentation and a bit out of Kazakhstan, but still smooth the output of a random number
Guess
Guessing game that allows players to enter the number of non-stop, just tell him you guessed big, guess a little, until the player guess
We know how output can generate random numbers, there is still input
Because vscode integrated console does not accept input, we want to debug input, the first thing is to open an external console
Write a loop, stop stdin (). Read_line
Complete version here
https://gitee.com/lightsever/rust_study/blob/master/hello02/src/main.rs
The new code appears only on this part
u32 u32 :: from_str method is converted from a string
But his return is the type of result
Here we use features syntax match rust to deal with him
Before .expect ( "") can quickly deal with this situation.
This is also to understand the error handling mechanism Rust, testing a variety of writing
Because we do use the return value processing branch, before .expect ( "") method is not good enough.