First day of rust study
Local Documentation
The installation of Rust also includes a copy of the documentation locally, so you can read it offline. Run rustup doc to open the local documentation in your browser.
Any time a type or function is provided by the standard library and you’re not sure what it does or how to use it, use the application programming interface (API) documentation to find out!
Writing and Running a Rust Program
fn main() {
println!("Hello, world!");
}
On Windows, enter the command .\main.exe instead of ./main:
Anatomy of a Rust Program
#![allow(unused)]
fn main() {
println!("Hello, world!");
}
This line does all the work in this little program: it prints text to the screen. There are four important details to notice here.
-
First, Rust style is to indent with four spaces, not a tab.
-
Second, println! calls a Rust macro. If it called a function instead, it would be entered as println (without the !). We’ll discuss Rust macros in more detail in Chapter 19. For now, you just need to know that using a ! means that you’re calling a macro instead of a normal function, and that macros don’t always follow the same rules as functions.
-
Third, you see the "Hello, world!" string. We pass this string as an argument to println!, and the string is printed to the screen.
-
Fourth, we end the line with a semicolon (;), which indicates that this expression is over and the next one is ready to begin. Most lines of Rust code end with a semicolon.
Compiling and Running Are Separate Steps
You’ve just run a newly created program, so let’s examine each step in the process.
Before running a Rust program, you must compile it using the Rust compiler by entering the rustc command and passing it the name of your source file, like this:
$ rustc main.rs

查看6道真题和解析
