https://github.com/lencx/learn-rust
🦀 Learning Rust
https://github.com/lencx/learn-rust
chapters example rust
Last synced: about 1 month ago
JSON representation
🦀 Learning Rust
- Host: GitHub
- URL: https://github.com/lencx/learn-rust
- Owner: lencx
- License: mit
- Created: 2018-12-16T10:35:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-24T13:48:49.000Z (over 5 years ago)
- Last Synced: 2025-06-30T17:46:50.497Z (9 months ago)
- Topics: chapters, example, rust
- Language: Rust
- Homepage: https://www.rust-lang.org
- Size: 279 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rust Learn
## Start
### Install
```bash
# installing `rustup` on Linux or macOS
curl https://sh.rustup.rs -sSf | sh
export PATH="$HOME/.cargo/bin:$PATH"
```
### Updating & Uninstalling
```bash
rustup update
rustup self uninstall
```
### Troubleshooting
```bash
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
# run only the ignored tests
cargo test -- --ignored
# to run all the tests in a particular integration test file
cargo test --test
# run tests for one particular crate in a workspace from the top-level directory
cargo test -p
cargo login
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 [--undo]
cargo install
```
> [Cargo.toml](https://doc.rust-lang.org/cargo/reference/manifest.html)
```toml
[package]
name = "pack_name" # the name of the package
version = "pack_version" # the current version, obeying semver
authors = ["name "]
edition = "2018"
[profile.dev]
opt-level = 0
[profile.release]
opt-level = 3
[dependencies]
[workspace]
members = ["..."]
```
### Local Documentation
```bash
rustup doc
```
## Data Types
* 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): The `f32` type is a single-precision float, and `f64` 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]`.
## Documentation
> 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.
## Attributes
```rust
/** 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
```rust
/** 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]
```
## Concept
* `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.
## Related Links
* [A closer look at Ownership in Rusts](https://blog.thoughtram.io/ownership-in-rust)