π¦ Learning Rust
https://github.com/lencx/learn-rust.git
# 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>
Cargo.toml
[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
isize & usize: 64 bits (64-bit architecture), 32 bits (32-bit architecture)*
f32,f64(default): Thef32type is a single-precision float, andf64has double precision.
boolchartupletuple: A finite heterogeneous sequence, (T, U, ..).
array slicearray: 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].
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 a Result, 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 is unsafe 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 | |
| procmacroderive | defines a derive macro | |
| procmacroattribute | 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 |
| noimplicitprelude | 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 |
| typelengthlimit | 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. \