标签:passing present tor syn The turn hose ber modules
创建项目
[root@itoracle test]# cargo new guessing_game
Created binary (application) `guessing_game` package
[root@itoracle test]# cd guessing_game
[root@itoracle guessing_game]# ls
Cargo.toml src
[root@itoracle guessing_game]# cat Cargo.toml
[package]
name = "guessing_game"
version = "0.1.0"
authors = ["tanpf <itoracle@163.com>"]
edition = "2018"
[dependencies]
the main
function is the entry point into the program
# vim src/main.rs
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
println!
is a macro that prints a string to the screen, then create a place to store the user input
Notice that this is a let
statement, which is used to create a variable. In Rust, variables are immutable by default. The following example shows how to use mut
before the variable name to make a variable mutable:
The next part of the code, .read_line(&mut guess)
, calls the read_line
method on the standard input handle to get input from the user. We’re also passing one argument to read_line
: &mut guess
.
The &
indicates that this argument is a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times. References are a complex feature, and one of Rust’s major advantages is how safe and easy it is to use references. You don’t need to know a lot of those details to finish this program. For now, all you need to know is that like variables, references are immutable by default. Hence, you need to write &mut guess
rather than &guess
to make it mutable.
.expect("Failed to read line");
As mentioned earlier, read_line
puts what the user types into the string we’re passing it, but it also returns a value—in this case, an io::Result
. Rust has a number of types named Result
in its standard library: a generic Result
as well as specific versions for submodules, such as io::Result
.
The Result
types are enumerations, often referred to as enums. An enumeration is a type that can have a fixed set of values, and those values are called the enum’s variants.
An instance of io::Result
has an expect
method that you can call. If this instance of io::Result
is an Err
value, expect
will cause the program to crash and display the message that you passed as an argument to expect
. If the read_line
method returns an Err
, it would likely be the result of an error coming from the underlying operating system. If this instance of io::Result
is an Ok
value, expect
will take the return value that Ok
is holding and return just that value to you so you can use it. In this case, that value is the number of bytes in what the user entered into standard input.
If you don’t call expect
, the program will compile, but you’ll get a warning.
println!("You guessed: {}", guess);
let x = 5;
let y = 10;
println!("x = {} and y = {}", x, y);
This code would print x = 5 and y = 10
.
rst003_guessing game
标签:passing present tor syn The turn hose ber modules
原文地址:https://www.cnblogs.com/perfei/p/10276554.html