Getting started with Rust
January 27, 2024
- download - https://www.rust-lang.org/tools/install
- book - https://doc.rust-lang.org/book/title-page.html
- Rust by examples - https://doc.rust-lang.org/rust-by-example/index.html
- cargo book - https://doc.rust-lang.org/cargo/index.html
- VSCode integration - https://code.visualstudio.com/docs/languages/rust (use
rust-analyzer
) - https://github.com/japaric/trust - test Rust crate on different architectures
- https://github.com/mehcode/config-rs/tree/master - layered config
- https://github.com/rust-unofficial/awesome-rust
- starter project examples
Syntax:
main
function is special, first code that runs in every rust executable- curly braces for functions
- semi-colons for expressions
rustfmt
included as part of the installation!
used for macros
Compilation:
- uses
rustc
- compiler for the Rust programming language, provided by the project itself, which takes your source code and produces binary code, either as a library or executable
- we only ever pass
rustc
the crate root, not every file we wish to compile
Packages and crates:
- a crate is a compilation unit in Rust (synonymous with packages from other languages)
- it is a library or executable program
- can contain modules, which may be defined in other files
- crates are shipped with Cargo, package management tool
- implicit root module, from which compilation starts
- modules allow you to partition your code within the crate itself
- binary crates
- programs that can be compiled to executables and run, including a
main
function
- programs that can be compiled to executables and run, including a
- library crates
- programs that don't compile to executablesm, don't define a
main
function, define functionality intended to be run by multiple projects
- programs that don't compile to executablesm, don't define a
- a package is a bundle of one or more crates
- contains a
Cargo.toml
file that describes how to build those crates - manifest file, which contains all the metadata needed to compile package
- contains a
- publishing crates to crates.io - https://crates.io/
Creating a new project:
cargo new my-project
- crate root -> module -> submodule
- crate root (
src/lib.rs
for library crate,src/main.rs
for binary crate) - module
- submodule
- modules declared with
mod
statements - privacy rules allow code references anywhere in crate
- code within a module is private from parent by default, use
pub
before decorations - https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
Commands:
cargo new hello-world --bin
cargo new my-lib --lib // different directory structure with test case to run
cargo test -- --nocapture
cargo build // target/debug
cargo run
cargo build --release // target/release
cargo update // regenerates Cargo.lock
cargo doc --open
Scope:
- prelude is brought into scope always - https://doc.rust-lang.org/std/prelude/index.html
Mutability:
let apples = 5; // immutable
let mut bananas = 5; // mutable
Configuration:
// to prevent SSL errors
// C:\Users\lmiloszewski\.cargo\config.toml
[http]
check-revoke = false