mirror of
https://github.com/theoludwig/rust_book.git
synced 2024-12-08 00:45:41 +01:00
.. | ||
src | ||
Cargo.lock | ||
Cargo.toml | ||
README.md |
1. Getting Started
To install Rust, we use rustup
, a command line tool for managing Rust versions and associated tools.
Hello, world
Rust use snake_case
as convention for naming variables, functions, modules, etc.
Filename: main.rs
fn main() {
println!("Hello, world!");
}
Compile and run:
rustc main.rs
./main
Cargo commands
=> Cargo as Convention.
cargo new hello_world
to create a new project calledhello_world
:--bin
to create a binary project (default),--lib
to create a library projectcargo build
to compile (development)./target/debug/hello_world
to execute (development)cargo run
to compile and execute (development)cargo test
to run testscargo doc --open
to generate documentation provided by all your dependencies locally and open it in your browsercargo check
checks your code to make sure it compiles but doesn't produce an executablecargo clippy
runs the clippy (official) linter (is a superset ofcargo check
, so it also checks for compilation errors),cargo clippy --fix
to automatically fix some of the errors/warningscargo fmt
to format the codecargo build --release
to compile with optimizations (production)./target/release/hello_world
to execute (production)cargo run --release
to compile and run (production)
Typical CI pipeline for Rust with Cargo
cargo build --verbose
cargo test --verbose
cargo clippy --verbose -- -D warnings
cargo fmt -- --check