Skip to content

Commit 5e8d3b5

Browse files
committed
πŸ§‘β€πŸ’» Update first sections
1 parent 403cbe2 commit 5e8d3b5

7 files changed

+91
-40
lines changed

β€Žcontent/en/docs/_overview.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ aliases:
55
- "/docs"
66
---
77

8-
This publication has its origins in the posts I authored on Medium at https://medium.com/learning-rust. However, please note that I have ceased updating the Medium posts. All current and future updates, new content, code, and grammar fixes will be exclusively maintained and released here, https://learning-rust.github.io.
9-
10-
[![Rust Playground](/docs/learning_rust_medium.png)](https://medium.com/learning-rust)
8+
## About me
119

1210
> πŸ§‘β€πŸ’» I am an expat working in Singapore as a Go Backend and DevOps Engineer. Feel free to reach out if you find any mistakes or anything that needs to be changed, including spelling or grammar errors. Alternatively, you can create a pull request, open an issue, or [share your awesome ideas in this gist](https://gist.github.com/dumindu/00a0be2d175ed5ff3bc3c17bbf1ca5b6). Good luck with learning Rust!
1311
14-
[![buymeacoffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-dumindu-FFDD00?style=for-the-badge&logo=buymeacoffee&logoColor=ffffff&labelColor=333333)](https://www.buymeacoffee.com/dumindu)
12+
[![learning-rust.github.io](https://img.shields.io/github/stars/learning-rust/learning-rust.github.io?style=for-the-badge&logo=rust&label=learning-rust.github.io&logoColor=333333&labelColor=f9f9f9&color=F46623)](https://github.com/learning-rust/learning-rust.github.io)
13+
[![learning-cloud-native-go.github.io](https://img.shields.io/github/stars/learning-cloud-native-go/learning-cloud-native-go.github.io?style=for-the-badge&logo=go&logoColor=333333&label=learning-cloud-native-go.github.io&labelColor=f9f9f9&color=00ADD8)](https://learning-cloud-native-go.github.io)
14+
15+
[![github.com](https://img.shields.io/badge/dumindu-866ee7?style=for-the-badge&logo=GitHub&logoColor=333333&labelColor=f9f9f9)](https://github.com/dumindu)
16+
[![buymeacoffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-dumindu-FFDD00?style=for-the-badge&logo=buymeacoffee&logoColor=333333&labelColor=f9f9f9)](https://www.buymeacoffee.com/dumindu)
17+
18+
## Overview
19+
20+
This publication has its origins in the posts I authored on Medium at https://medium.com/learning-rust. However, please note that I have ceased updating the Medium posts. All current and future updates, new content, code, and grammar fixes will be exclusively maintained and released here, https://learning-rust.github.io.
21+
22+
[![Learning Rust @Medium](/docs/learning_rust_medium.png)](https://medium.com/learning-rust)

β€Žcontent/en/docs/a1.why-rust.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ Its design elements came from a wide range of sources.
3636
and etc.
3737

3838

39-
Rust **doesn't use an automated garbage collection** system\(GC\) by default.
39+
Rust **doesn't use a built-in runtime** or an automated garbage collection system \(GC\).
4040

41-
Rust compiler observes the code **at compile-time** and helps to [**prevent many types of errors**](https://doc.rust-lang.org/error-index.html) that are possible to write in C, C++ like programming languages.
41+
> πŸ’‘However, async Rust requires an async runtime, which is provided by community-maintained crates like [`tokio`](https://github.com/tokio-rs/tokio), [`async-std`](https://github.com/async-rs/async-std), [`soml`](https://github.com/smol-rs/smol) etc. The async runtime will be bundled into the final executable.
42+
43+
Rust compiler **observes the code at compile-time** and helps to [prevent many types of errors](https://doc.rust-lang.org/error-index.html) that are possible to write in C, C++ like programming languages.
4244

4345
## πŸ‘¨β€πŸ« Before going to the next...
4446

β€Žcontent/en/docs/a2.installation.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ slug: installation
66
## Rustup
77
There are many ways to install Rust on your system. For the moment the official way to install Rust is using [Rustup](https://rustup.rs/).
88

9-
[πŸ“–](https://rust-lang.github.io/rustup/index.html) Rustup installs The Rust Programming Language from the official release channels, enabling you to easily switch between **stable, beta, and nightly** compilers and keep them updated. It makes **cross-compiling** simpler with binary builds of the standard library for common platforms.
9+
[πŸ“–](https://rust-lang.github.io/rustup/index.html) Rustup installs The Rust Programming Language from the official release channels, enabling you to easily switch between **stable, beta, and nightly** compilers and keep them updated. It also makes cross-compiling simpler with binary builds of the standard library for common platforms.
1010

1111
[πŸ“–](https://rust-lang.github.io/rustup/installation/index.html) Rustup installs **`rustc`, `cargo`, `rustup`** and other standard tools to Cargo's `bin` directory. On Unix it is located at `$HOME/.cargo/bin` and on Windows at `%USERPROFILE%\.cargo\bin`. This is the same directory that `cargo install` will install Rust programs and Cargo plugins.
1212

@@ -18,8 +18,9 @@ There are many ways to install Rust on your system. For the moment the official
1818
> - `cargo-fmt`: Helps to run `rustfmt` on whole Rust projects, including multi-crate workspaces.
1919
> - `cargo-clippy`: A lint tool that provides extra checks for common mistakes and stylistic choices.
2020
> - `cargo-miri`:An experimental Rust interpreter, which can be used for checking for undefined-behavior.
21-
> - `rls`: A language server that provides support for editors and IDEs.
2221
> - `rustdoc`: A local copy of the Rust documentation.
22+
> - `rust-analyzer`: A language server that provides support for editors and IDEs.
23+
> - `rust-gdb`: A debugger that wraps GNU Debugger(GDB).
2324
2425
## Installation
2526

@@ -31,10 +32,11 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
3132
### For Windows Users
3233
Download **`rustup-init.exe`** from [www.rustup.rs](https://rustup.rs/) and run.
3334

34-
> ⭐ If you are on **Microsoft Windows**, you have to install **[Visual C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/)** 2015 or higher, which requires an additional 3–4 GBs.
35+
> πŸ’‘ You may need to install [Visual C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) 2015 or higher, which requires an additional 3–4 GBs.
3536
3637
## πŸ‘¨β€πŸ« Before going to the next...
3738

38-
- To verify the current Rust version, use the **`rustc --version`** or **`rustc -V`** command.
39-
- Rust ships releases on **six week cycles**. When a new Rust version released, use the **`rustup update`** command to update the Rust ecosystem.
40-
- To open the offline Rust documentation, use the **`rustup doc`** command. For more `rustup` commands, check the **`rustup --help`** command.
39+
- To verify the current Rust version, use the **`rustc --version`** or shorter form`rustc -V` command.
40+
- Rust follows **six week** release cycles. Use the **`rustup update`** command to update the Rust ecosystem.
41+
- You can access Rust's offline documentation via the `rustup doc` command.
42+
- For a full list of `rustup` commands, refer to the `rustup --help` command.

β€Žcontent/en/docs/a3.hello-world.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ fn main() {
6161
```
6262

6363
- Check the [difference between macros and functions](https://doc.rust-lang.org/book/ch19-06-macros.html#the-difference-between-macros-and-functions).
64-
- For more `rustc` commands, check the **`rustc --help`** command.
64+
- For more `rustc` commands, check the `rustc --help` command.

β€Žcontent/en/docs/a4.cargo-crates-and-basic-project-structure.md

+66-27
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,61 @@ slug: cargo-crates-and-basic-project-structure
55

66
## Cargo
77

8-
Cargo is Rust’s built-in package manager and the build system. It can be used to,
8+
Cargo is Rust’s built-in package manager and build system. It also supports the following actions,
99

10-
- Create a new project: `cargo new`
11-
- Create a new project in an existing directory: `cargo init`
12-
- Build the project: `cargo build`
13-
- Run the project: `cargo run`
14-
- Update project dependencies: `cargo update`
15-
- Run tests: `cargo test`
16-
- Run benchmarks: `cargo bench`
17-
- Generate the project documentation via [rustdoc](https://doc.rust-lang.org/stable/rustdoc/): `cargo doc`
18-
- Analyze the project to see it has any errors, without building it: `cargo check`
10+
| Command | Action |
11+
| ------------- | --------------------------------------------- |
12+
| `cargo new` | Create a new project |
13+
| `cargo init` | Create a new project in an existing directory |
14+
| `cargo check` | Verify the project compiles without errors |
15+
| `cargo build` | Build the executable |
16+
| `cargo run` | Build the executable and run |
1917

20-
In addition, there are `cargo` commands to publish the project as a crate/ package to **Rust's official crate registry, [crates.io](https://crates.io/)**.
18+
> πŸ’‘ The `cargo check` command verifies that the project compiles without errors, without producing an executable.
19+
> Thus, it is often faster than `cargo build`.
2120
22-
> πŸ’‘ We need to get an API token from [crates.io](https://crates.io/) to publish a crate to it. The API token can be found in the [Account Settings page](https://crates.io/me), after login to that site. We will discuss more about this under [code organization with crates](/docs/crates#c-using-cratesio).
21+
> πŸ’‘ Cargo places executables compiled with `cargo build` or `cargo run` in the `target/debug/` directory.
22+
> But, while those built with **`cargo build --release`** for release purposes are stored in `target/release/` directory.
23+
> Release builds use more optimizations and remove some runtime safety checks to increase performance, although this comes at the cost of longer compile time.
2324
24-
- Login to [crates.io](https://crates.io/) with the API token: `cargo login`
25-
- Make the local crate uploadable to [crates.io](https://crates.io/): `cargo package`
26-
- Upload the crate to [crates.io](https://crates.io/): `cargo publish`
27-
- Install a Rust binary: `cargo install`
28-
- Uninstall a Rust binary: `cargo uninstall`
25+
| Command | Action |
26+
|----------------|---------------------------------------------------|
27+
| `cargo add` | Add a dependency crate to the project |
28+
| `cargo remove` | Remove a dependency crate from the project |
29+
| `cargo fetch` | Download the dependencies specified in Cargo.lock |
30+
| `cargo update` | Update project dependencies |
31+
32+
33+
> πŸ’‘ A crate is a package that can be shared via [crates.io](https://crates.io), Rust community’s crate registry.
34+
> `cargo add`, `cargo remove`, `cargo fetch`, and `cargo update` commands manage project dependencies through the crate hosted on crates.io.
35+
36+
> πŸ’‘ The `cargo add` command includes a specified crate in the `[dependencies]` section of `Cargo.toml`, while `cargo add --dev` adds a crate to the `[dev-dependencies]` section. This indicates that the crate is only used for development purposes like testing and will not be included in the final compiled code.
37+
38+
| Command | Action |
39+
|---------------|---------------------------------------------------------------------------------------------|
40+
| `cargo test` | Run tests |
41+
| `cargo bench` | Run benchmarks |
42+
| `cargo doc` | Generate the project documentation via [rustdoc](https://doc.rust-lang.org/stable/rustdoc/) |
43+
44+
In addition, there are `cargo` commands to publish the project as a crate to [crates.io](https://crates.io/).
45+
46+
| Command | Action |
47+
| ----------------- |--------------------------------------------------------------------|
48+
| `cargo login` | Login to [crates.io](https://crates.io/) with the API token |
49+
| `cargo package` | Make the local crate uploadable to [crates.io](https://crates.io/) |
50+
| `cargo publish` | Upload the crate to [crates.io](https://crates.io/) |
51+
| `cargo install` | Install a Rust binary |
52+
| `cargo uninstall` | Uninstall a Rust binary |
53+
54+
> πŸ’‘ You need to get an API token from [crates.io](https://crates.io/) to publish a crate to it. The API token can be found in the [Account Settings page](https://crates.io/me), after login to that site. We will discuss more about this under [code organization with crates](/docs/crates#c-using-cratesio).
2955
3056
## Crate
3157

32-
A crate is a package, which can be shared via [crates.io](https://crates.io/). A crate can produce an executable or a library. In other words, it can be a **binary** crate or a **library** crate.
58+
- A crate is a package, which can be shared via Rust community’s crate registry, [crates.io](https://crates.io/).
3359

34-
01. `cargo new crate_name --bin` or `cargo new crate_name`: Produces an executable
35-
02. `cargo new crate_name --lib`: Produces a library
60+
- A crate can produce an executable or a library. In other words, it can be a **binary** crate or a **library** crate.
61+
1. `cargo new crate_name --bin` or `cargo new crate_name`: Produces an executable
62+
2. `cargo new crate_name --lib`: Produces a library
3663

3764
The first one generates,
3865

@@ -54,8 +81,6 @@ and the second one generates,
5481
- **src** folder is the place to store the source code.
5582
- Each crate has an implicit crate root/ entry point. **main.rs** is the crate root for a binary crate and **lib.rs** is the crate root for a library crate.
5683

57-
> πŸ’‘ When we build a binary crate via `cargo build` or `cargo run`, the executable file will be stored in the **target/debug/** folder. But when building it via **`cargo build --release`** for a release it will be stored in the **target/release/** folder. The release builds are applying more optimizations while compiling the code, to make the code run faster. But it takes more compile time.
58-
5984
## Project Structure
6085

6186
This is how [Cargo documentation describes](https://doc.rust-lang.org/cargo/guide/project-layout.html) about the recommended project layout,
@@ -68,7 +93,10 @@ This is how [Cargo documentation describes](https://doc.rust-lang.org/cargo/guid
6893
β”‚ β”œβ”€β”€ main.rs
6994
β”‚ β”œβ”€β”€ lib.rs
7095
β”‚ └── bin
71-
β”‚ └── another_executable.rs
96+
β”‚ β”œβ”€β”€ another_executable.rs
97+
β”‚ └── multi_file_executable
98+
β”‚ β”œβ”€β”€ main.rs
99+
β”‚ └── some_module.rs
72100
β”œβ”€β”€ tests
73101
β”‚ └── some_integration_tests.rs
74102
β”œβ”€β”€ benches
@@ -80,20 +108,31 @@ This is how [Cargo documentation describes](https://doc.rust-lang.org/cargo/guid
80108
- The source code goes in the `src` directory.
81109
- The default executable file is `src/main.rs`.
82110
- The default library file is `src/lib.rs`.
83-
- Other executables can be placed in `src/bin/*.rs`.
111+
- Other executables can be placed in,
112+
- `src/bin/*.rs`
113+
- `src/bin/*/main.rs`
84114
- Integration tests go in the `tests` directory \(unit tests go in each file they're testing\).
85115
- Benchmarks go in the `benches` directory.
86116
- Examples go in the `examples` directory.
87117

88118
## Rust Editions
89119

90-
After the initial release in 2015, according to the feedback got from user communities, the Rust team was focusing to increase the **productivity** of the language and the ecosystem. After 3 years of hard work in 2018, a new Rust edition was released with new features, simplified syntax and better tooling. We call it **Rust 2018** edition.
120+
The language has seen a series of improvements every three years through new editions since its initial stable release in 2015, including the initial version, **Rust 2015**, followed by **Rust 2018**, and the latest, **Rust 2021**.
121+
122+
The `edition` key in the `Cargo.toml` file denotes the edition of the Rust compiler to be used for compiling the crate. Editions are opt-in, meaning existing crates will not see these changes until they explicitly migrate to the new edition. Rust guarantees backward compatibility between editions, allowing crates using older editions of Rust to interoperate seamlessly with those using newer versions.
91123

92-
To keep the promise of supporting backward compatibility, the new `edition = "2018"` configuration was added to the `Cargo.toml` file. For new projects, the `cargo new` command adds this configuration by default. So, you don't need to care. But on legacy crates, if you can not see any `edition` configuration, Cargo will consider it as a Rust 2015 edition crate.
124+
For new projects created by `cargo new`, it will set `edition = "2021"` by default in the `Cargo.toml` file. For example,
125+
126+
```toml
127+
[package]
128+
name = "api"
129+
version = "0.1.0"
130+
edition = "2021"
131+
```
93132

94133
## πŸ‘¨β€πŸ« Before going to the next...
95134

96-
- The **`.cargo/bin` directory of your home directory** is the default location of Rust binaries. Not only the official binaries like `rustup`, `rustc`, `cargo`, `rustfmt`, `rustdoc`, `rls` and also the binaries you can install via `cargo install` command, will be stored in this directory.
135+
- The `.cargo/bin` directory of your home directory is the default location of Rust binaries. Not only the official binaries like `rustc`, `cargo`, `rustup`, `rustfmt`, `rustdoc`, `rust-analyzer` and also the binaries you can install via `cargo install` command, will be stored in this directory.
97136

98137
- Even though the initial convention for naming crates and file names is using the [`snake_case`](https://en.wikipedia.org/wiki/Snake_case), some crate developers are using `kebab-case` on both crates and file names. To make your code more consistent, use the initial convention `snake_case`; especially on file names.
99138

-44.7 KB
Loading
-13 KB
Loading

0 commit comments

Comments
Β (0)