Rust
Rust programming language is focusing on performance, security and safety. Get started on the rust-lang.org website.
Tooling
Rustup
Install rust or, if already installed but outdated, update with rustup update
.
rustc
Compile with rustc main.rs
.
Cargo
Cargo handles crates. A crate can be binary or library crate.
Create a new project with cargo new
.
Build project with cargo build
and then run the project with ./target/debug/[project_name]
. If you want to create a optimized version of the build then do cargo build --release
.
Run project with cargo run
.
Check a project for errors with cargo check
.
The cargo lock file stores the complete dependency tree for the build. It is a good idea to keep that under version control. To update the dependency tree, first update the dependencies section in the Cargo.toml
file and then run cargo update
.
Prelude
Definition of rust prelude. Other packages can contain their own preludes.
Types
Bring a type into the program scope with use
. For example, use std::io;
.
Constants
Constants are always immutable and are declared with const
and must be annotated.
Constants can be declared in the global scope.
Constants may be set only to a constant expression, not the result of a value that could only be computed at runtime.
Rust’s naming convention for constants is to use all uppercase with underscores between words.
Importing
Use use
or call directly with, for example, std::io::Stdin
.
Functions
Functions can return a result.
Result
Result is an enumeration, also called as enum. An enum is a type which can be in one of multiple possible states. One of the possible states is called a variant.
The result variants are Ok
and Err
. The Ok variant indicates the operation was successful, and inside Ok is the successfully generated value. The Err variant means the operation failed, and Err contains information about how or why the operation failed. If this instance of Result is an Err value, expect will cause the program to crash and display the message that you passed as an argument to expect. If the readline method returns an Err, it would likely be the result of an error coming from the underlying operating system. If this instance of Result is an Ok value, expect will take the return value that Ok is holding and return just that value to you so you can use it. In this case, that value is the number of bytes in the user’s input.
Error handling
You can handle errors with a "match" statement.
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue, // underscorde (_) is a catchall value.
}
Associated Functions
The syntax ::[function_name]
indicates that "functionname" is an associated function. For example let mut variable = String::new();
, is creating a mutable variable of type String with the function "new".
Control Flow
loop
The loop
keyword creates an infinite loop.
match
The match expression is made up of arms. An arm consits for a pattern to match against and the code that should be run if the value given to match fits the arm's pattern.
References
Use the sign "&
" to indicate a reference, which gives you a way to let multiple parts of your code access one piece of data without needing to copy that data into memory multiple times. References are immutable by default.