Rust学习——TRPL-Part2
The Rust Programming Language
系列文章学习 Part 2
Installation and Hello World
Updating and Uninstalling
After you've installed Rust via rustup
, to update:
1 |
|
To uninstall Rust and rustup
1 |
|
Hello World Program
1 |
|
rustfmt
formatter:Provide standard style across Rust projects. The Rust team has included this tool with the standard Rust distribution, like
rustc
. Use it!
1 |
|
4 import details to notice:
- First, Rust style to indent with four spaces, not a tab
println!
- Rust macro. Using a!
means to call a macro instead of a normal function. (Detail in Chapter 19)- Pass a string as argument.
- End with a semicolon(
;
).
Hello, Cargo!
Cargo is Rust's build system and package manager. Most Rustaceans use this tool to manage their Rust projects.
- Project building
- Download libraries and build them(dependencies.)
1 |
|
- a Cargo.toml file: TOML format style configuration file
- a src directory: source files source code
- a .git Git repository along with a .gitignore file
Move into Cargo
If you started a project that doesn't use Cargo,as we did with the "Hello, world" project, you can conver it to a project that does use Cargo.
Move the project code into the src directory and create an appropriate Cargo.toml file.
Cargo Run and Cargo Check
cargo build
build a project. cargo run
can compile the code and then run the resulting(build and directly run in ./target/debug/hello_cargo
).
Cargo Check cargo check
, quickly checks your code without producing an executable.
Why not want an executable?
Speed up. Often,
cargo check
is much faster thancargo build
, because it skips the step of produing an executable. Frequentlly use when continually checking your work while writing the code.
Recap what we've learned so far about Cargo"
- We can build a project using
cargo build
. - We can build and run a project in one step using
cargo run
. - We can build a project without producing a binary to check for errors using
cargo check
- Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
Building for Release
Use cargo build --release
to compile it with optimizations, create an executable in target/release instead of target/debug. The optimizations make your Rust code run faster(but take longer time to compile).
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!