Input and output
As with C++, there are standard input and output:
use std::io::stdin; fn main() { println! (" Hello, what's your name?" ); let mut your_name = String::new(); stdin() .read_line(&mut your_name) .expect("Failed to read line"); println! (" Hello, {}", your_name)}Copy the code
For those who have experience in development, or who know C++, this code will not be able to understand, in a word: it is to use the standard STD :: IO ::stdin method to enter such as, then store the content in the variable your_name, read, and finally add an exception expect.
As for the writing method of this chain, it is explained in the article:
Combining functions like this is called function chaining. Starting from the top, each function passes its results to the next function. It’s common to format a function chain with each step on its line, indented to indicate that the block belongs together.
function
Encapsulate the above code into a function:
use std::io::stdin; fn what_is_your_name() -> String { let mut your_name = String::new(); stdin() .read_line(&mut your_name) .expect("Failed to read line"); your_name } fn main() { println! (" Hello, what's your name?" ); let name = what_is_your_name(); println! (" Hello, {}", name); }Copy the code
The special difference is that the return from a function can be simply your_name, omitting the return and semicolon, which is interesting.
This line doesn’t end with a semicolon. This is Rust shorthand for return. Any expression may return This way same as typing return your_name; Clippy will complain if you type return when you don’t need it.
Array
There are two rules for defining arrays:
- Consistent data type;
- The length of the array stays the same
The methods and forms used to define arrays and convenience arrays are similar to those used in other languages.
Let visitor_list = [" yE mei tree ", "ye shuai "," Ye ge "]; println! (" First traversal method "); for i in 0.. visitor_list.len() { println! ("{}", visitor_list[i]); } println! (" Second traversal method "); for visitor in &visitor_list { println! ("{}", visitor); }Copy the code
Structs
Saying: structure
The String and StdIn we used above are struct types.
We can define a structure and then inherit that structure and write the corresponding structure method, kind of like Swift.
For example, we define a Visitor structure:
Struct Visitor {name: String, greeting: String,} Visitor {fn new(name: &str, greeting: String); &str) -> Self { Self { name: name.to_lowercase(), greeting: greeting.to_string(), } } fn greet_visitor(&self) { println! ("{}", self.greeting); }}Copy the code
Use:
use std::io::stdin; fn what_is_your_name() -> String { let mut your_name = String::new(); stdin() .read_line(&mut your_name) .expect("Failed to read line"); Struct Visitor {name: String, greeting: String,} Visitor {fn new(name: fn); &str, greeting: &str) -> Self { Self { name: name.to_string(), greeting: greeting.to_string(), } } fn greet_visitor(&self) { println! ("{}", self.greeting); } } fn main() { let visitor_list = [ Visitor::new("bert", "Hello Bert, enjoy your treehouse."), Visitor::new("steve", "Hi Steve. Your milk is in the fridge."), Visitor::new("fred", "Wow, who invited Fred?"), ]; println! (" Hello, what's your name?" ); let name = what_is_your_name(); let known_visitor = visitor_list .iter() .find(|visitor| visitor.name == name); match known_visitor { Some(visitor) => visitor.greet_visitor(), None => println! (" not in the list ")}}Copy the code
Vectors
Compared to Arrays, Vectors can dynamically resize, adding elements using push methods until they reach the limit of system memory, or increase them indefinitely.
Other methods of use are similar and will not be described here.
Enumerations
This use is similar, no different, in the future use of the process to describe how to use.
enum VisitorAction {
Accept,
AcceptWithNote { note: String },
Refuse,
Probation,
}
Copy the code
Here, AcceptWithNodte {note: String} when you use it, you can use custom variables.
For the second chapter, the core of the basic on these, the third chapter we can enter the Game stage, the above basic knowledge we can have targeted at the Internet to view the specific use method.