A repository to track my progress learning Rust, primarily following 'The Rust Programming Language': https://doc.rust-lang.org/stable/book/
Instructions for installing Rust can be found here.
Rust uses the file extension .rs. To compile a file, we run rustc [filename].rs
, and to run the resulting binary, we use ./[filename]
.
Cargo is a useful tool that handles various tasks like building your code and downloading the libraries that your code depends on.
To initialise Cargo in a new folder as a Git repository, run cargo new [folder-name]
. You can add the flag --vcs=none
(as in cargo new --vcs=none [folder-name]
) to avoid creating the Git repository. Cargo can also be initialised in an existing folder using cargo init
.
This creates a project with a Cargo.toml
file which contains metadata for the project and keeps track of the dependencies used. It will also create a main.rs
file in the src
folder, which is the generally the entry point of a program.
A crate in Rust is a compilation unit. Note that a crate can be compiled into a binary or into a library. Libraries have no entry point, but instead provide functionality for other crates to use. To create a library, add the --lib
flag to cargo new
.
To build an existing project, use cargo build
. Cargo stores the result of the build in the ./target/debug directory
. If you want to compile and run the project, use cargo run
.
Additionally, you can use cargo build --release
to build for production.
You can run cargo check
to check that the code compiles without building, and cargo doc --open
to see generated documentation.
- The Rust Programming Language: an introductory book about Rust
- Rust By Example: examples illustrating various Rust concepts
- The Cargo Book: learn more about Cargo
- Rustlings: small exercises for getting used to reading and writing Rust code