# installing `rustup` on Linux or macOS
curl https://sh.rustup.rs -sSf | sh
export PATH="$HOME/.cargo/bin:$PATH"
rustup update
rustup self uninstall
rustc
rustup
cargo
cargo new
cargo check
cargo run
cargo build [--release]
cargo doc [--open]
cargo test
# If you don’t want to run the tests in parallel
# or if you want more fine-grained control over the number of threads used
# We set the number of test threads to 1, telling the program not to use any parallelism.
cargo test -- --test-threads=1
# disable the output capture behavior
cargo test -- --nocapture
# Filtering to Run Multiple Tests
# note that the module in which a test appears becomes part of the test’s name,
# so we can run all the tests in a module by filtering on the module’s name.
cargo test <fn_part_name>
# run only the ignored tests
cargo test -- --ignored
# to run all the tests in a particular integration test file
cargo test --test <filename>
# run tests for one particular crate in a workspace from the top-level directory
cargo test -p <crate_name>
cargo login <API_TOKEN>
cargo publish
# `--undo`: By adding `--undo` to the command, you can also undo a yank and allow projects to start depending on a version again
cargo yank --vers <version> [--undo]
cargo install <binary_crate>
[package]
name = "pack_name" # the name of the package
version = "pack_version" # the current version, obeying semver
authors = ["name <email@example.com>"]
edition = "2018"
[profile.dev]
opt-level = 0
[profile.release]
opt-level = 3
[dependencies]
[workspace]
members = ["..."]
rustup doc
-
Integer Types
Length Signed Unsigned 8-bit i8 u8 16-bit i16 u16 32-bit i32 u32 64-bit i64 u64 128-bit i128 u128 arch isize usize - signed: -2n-1 ~ 2n-1-1
- unsigned: 0 ~ 2n-1
isize
&usize
: 64 bits (64-bit architecture), 32 bits (32-bit architecture)
-
Floating-Point Types
f32
,f64
(default): Thef32
type is a single-precision float, andf64
has double precision.
-
The Boolean Type:
bool
-
The Character Type:
char
-
Compound Types:
- The Tuple Type:
tuple
tuple
: A finite heterogeneous sequence, (T, U, ..).- The Array Type:
array
slice
array
: A fixed-size array, denoted[T; N]
, for the element type,T
, and the non-negative compile-time constant size,N
.slice
: A dynamically-sized view into a contiguous sequence,[T]
. - The Tuple Type:
Documentation comments within items are useful for describing crates and modules especially. Use them to explain the overall purpose of the container to help your users understand the crate’s organization.
///
: documentation comments, support Markdown notation for formatting the text.//!
: adds documentation to the item that contains the comments rather than adding documentation to the items following the comments.
Commonly Used Sections
Panics
: The scenarios in which the function being documented could panic. Callers of the function who don’t want their programs to panic should make sure they don’t call the function in these situations.Errors
: If the function returns aResult
, describing the kinds of errors that might occur and what conditions might cause those errors to be returned can be helpful to callers so they can write code to handle the different kinds of errors in different ways.Safety
: If the function isunsafe
to call, there should be a section explaining why the function is unsafe and covering the invariants that the function expects callers to uphold.
/** syntax */
// InnerAttribute:
#![Attr]
// OuterAttribute:
#[Attr]
Built-in attributes | attribute | description |
---|---|---|
Conditional compilation | cfg | controls conditional compilation |
cfg_attr | conditionally includes attributes | |
Testing | test | marks a function as a test |
ignore | disables a test function | |
should_panic | indicates a test should generate a panic. | |
Derive | derive | automatic trait implementations |
Macros | macro_export | exports a macro_rules macro for cross-crate usage |
macro_use | expands macro visibility, or imports macros from other crates | |
proc_macro | defines a function-like macro | |
proc_macro_derive | defines a derive macro | |
proc_macro_attribute | defines an attribute macro | |
Diagnostics | allow, warn, deny, forbid | alters the default lint level |
deprecated | generates deprecation notices | |
must_use | generates a lint for unused values | |
ABI, linking, symbols, and FFI | link | specifies a native library to link with an extern block |
link_name | specifies the name of the symbol for functions or statics in an extern block |
|
no_link | prevents linking an extern crate | |
repr | controls type layout | |
crate_type | specifies the type of crate (library, executable, etc.) | |
no_main | disables emitting the main symbol | |
export_name | specifies the exported symbol name for a function or static | |
link_section | specifies the section of an object file to use for a function or static | |
no_mangle | disables symbol name encoding | |
used | forces the compiler to keep a static item in the output object file | |
crate_name | specifies the crate name | |
Code generation | inline | hint to inline code |
cold | hint that a function is unlikely to be called | |
no_builtins | disables use of certain built-in functions | |
target_feature | configure platform-specific code generation | |
Documentation | doc | specifies documentation. See
The Rustdoc Book
for more information.
Doc comments
are transformed into doc attributes
|
Preludes | no_std | removes std from the prelude |
no_implicit_prelude | disables prelude lookups within a module | |
Modules | path | specifies the filename for a module |
Limits | recursion_limit | sets the maximum recursion limit for certain compile-time operations |
type_length_limit | sets the maximum size of a polymorphic type | |
Runtime | panic_handler | sets the function to handle panics |
global_allocator | sets the global memory allocator | |
windows_subsystem | specifies the windows subsystem to link with | |
Features | feature | used to enable unstable or experimental compiler features. See
The Unstable Book
for features implemented in rustc
|
/** Diagnostics */
// overrides the check for C so that violations will go unreported
#[allow(C)]
// warns about violations of C but continues compilation
#[warn(C)]
// signals an error after encountering a violation of C
#[deny(C)]
// is the same as deny(C), but also forbids changing the lint level afterwards
#[forbid(C)]
// since: specifies a version number when the item was deprecated
// note: specifies a string that should be included in the deprecation message
#[deprecated(since = "", note="")]
// is used to issue a diagnostic warning when a value is not "used"
#[must_use]
/** Code generation */
// suggests performing an inline expansion.
#[inline]
// suggests that an inline expansion should always be performed.
#[inline(always)]
// suggests that an inline expansion should never be performed.
#[inline(never)]
/** Testing */
#[test]
#[ignore]
#[should_panic]
Object-Oriented
: Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations.Polymorphism
: To many people, polymorphism is synonymous with inheritance. But it’s actually a more general concept that refers to code that can work with data of multiple types. For inheritance, those types are generally subclasses.
Rust instead uses generics to abstract over different possible types and trait bounds to impose constraints on what those types must provide. This is sometimes called bounded parametric polymorphism.