📦 lencx / learn-rust

📄 README.md · 409 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409# 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 <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](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 <email@example.com>"]
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: **-2<sup>n-1</sup> ~ 2<sup>n-1</sup>-1**
  * unsigned: **0 ~ 2<sup>n</sup>-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]
```

<table>
<thead>
  <tr>
    <th>Built-in attributes</th>
    <th>attribute</th>
    <th>description</th>
  </tr>
</thead>
<tbody>
  <!-- Conditional compilation -->
  <tr>
      <td rowspan="2">Conditional compilation</td>
      <td><b>cfg</b></td>
      <td>controls conditional compilation</td>
  </tr>
  <tr>
      <td><b>cfg_attr</b></td>
      <td>conditionally includes attributes</td>
  </tr>

  <!-- Testing -->
  <tr>
      <td rowspan="3">Testing</td>
      <td><b>test</b></td>
      <td>marks a function as a test</td>
  </tr>
  <tr>
      <td><b>ignore</b></td>
      <td>disables a test function</td>
  </tr>
  <tr>
      <td><b>should_panic</b></td>
      <td>indicates a test should generate a panic.</td>
  </tr>

  <!-- Testing -->
  <tr>
      <td rowspan="1">Derive</td>
      <td><b>derive</b></td>
      <td>automatic trait implementations</td>
  </tr>

  <!-- Macros -->
  <tr>
      <td rowspan="5">Macros</td>
      <td><b>macro_export</b></td>
      <td>exports a <code>macro_rules</code> macro for cross-crate usage</td>
  </tr>
  <tr>
      <td><b>macro_use</b></td>
      <td>expands macro visibility, or imports macros from other crates</td>
  </tr>
  <tr>
      <td><b>proc_macro</b></td>
      <td>defines a function-like macro</td>
  </tr>
  <tr>
      <td><b>proc_macro_derive</b></td>
      <td>defines a derive macro</td>
  </tr>
  <tr>
      <td><b>proc_macro_attribute</b></td>
      <td>defines an attribute macro</td>
  </tr>

  <!-- Diagnostics -->
  <tr>
      <td rowspan="3">Diagnostics</td>
      <td><b>allow, warn, deny, forbid</b></td>
      <td>alters the default lint level</td>
  </tr>
  <tr>
      <td><b>deprecated</b></td>
      <td>generates deprecation notices</td>
  </tr>
  <tr>
      <td><b>must_use</b></td>
      <td>generates a lint for unused values</td>
  </tr>

  <!-- ABI, linking, symbols, and FFI -->
  <tr>
      <td rowspan="11">ABI, linking, symbols, and FFI</td>
      <td><b>link</b></td>
      <td>specifies a native library to link with an <code>extern</code> block</td>
  </tr>
  <tr>
      <td><b>link_name</b></td>
      <td>specifies the name of the symbol for functions or statics in an <code>extern</code> block</td>
  </tr>
  <tr>
      <td><b>no_link</b></td>
      <td>prevents linking an extern crate</td>
  </tr>
  <tr>
      <td><b>repr</b></td>
      <td>controls type layout</td>
  </tr>
  <tr>
      <td><b>crate_type</b></td>
      <td>specifies the type of crate (library, executable, etc.)</td>
  </tr>
  <tr>
      <td><b>no_main</b></td>
      <td>disables emitting the main symbol</td>
  </tr>
  <tr>
      <td><b>export_name</b></td>
      <td>specifies the exported symbol name for a function or static</td>
  </tr>
  <tr>
      <td><b>link_section</b></td>
      <td>specifies the section of an object file to use for a function or static</td>
  </tr>
  <tr>
      <td><b>no_mangle</b></td>
      <td>disables symbol name encoding</td>
  </tr>
  <tr>
      <td><b>used</b></td>
      <td>forces the compiler to keep a static item in the output object file</td>
  </tr>
  <tr>
      <td><b>crate_name</b></td>
      <td>specifies the crate name</td>
  </tr>

  <!-- Code generation -->
  <tr>
      <td rowspan="4">Code generation</td>
      <td><b>inline</b></td>
      <td>hint to inline code</td>
  </tr>
  <tr>
      <td><b>cold</b></td>
      <td>hint that a function is unlikely to be called</td>
  </tr>
  <tr>
      <td><b>no_builtins</b></td>
      <td>disables use of certain built-in functions</td>
  </tr>
  <tr>
      <td><b>target_feature</b></td>
      <td>configure platform-specific code generation</td>
  </tr>

  <!-- Documentation -->
  <tr>
      <td rowspan="1">Documentation</td>
      <td><b>doc</b></td>
      <td>specifies documentation. See
        <a href="https://doc.rust-lang.org/rustdoc/the-doc-attribute.html">The Rustdoc Book</a>
        for more information.
        <a href="https://doc.rust-lang.org/reference/comments.html#doc-comments">Doc comments</a>
        are transformed into <code>doc</code> attributes
      </td>
  </tr>

  <!-- Preludes -->
  <tr>
      <td rowspan="2">Preludes</td>
      <td><b>no_std</b></td>
      <td>removes std from the prelude</td>
  </tr>
  <tr>
      <td><b>no_implicit_prelude</b></td>
      <td>disables prelude lookups within a module</td>
  </tr>

  <!-- Modules -->
  <tr>
      <td rowspan="1">Modules</td>
      <td><b>path</b></td>
      <td>specifies the filename for a module</td>
  </tr>

  <!-- Limits -->
  <tr>
      <td rowspan="2">Limits</td>
      <td><b>recursion_limit</b></td>
      <td>sets the maximum recursion limit for certain compile-time operations</td>
  </tr>
  <tr>
      <td><b>type_length_limit</b></td>
      <td>sets the maximum size of a polymorphic type</td>
  </tr>

  <!-- Runtime -->
  <tr>
      <td rowspan="3">Runtime</td>
      <td><b>panic_handler</b></td>
      <td>sets the function to handle panics</td>
  </tr>
  <tr>
      <td><b>global_allocator</b></td>
      <td>sets the global memory allocator</td>
  </tr>
  <tr>
      <td><b>windows_subsystem</b></td>
      <td>specifies the windows subsystem to link with</td>
  </tr>

  <!-- Features -->
  <tr>
      <td rowspan="1">Features</td>
      <td><b>feature</b></td>
      <td>used to enable unstable or experimental compiler features. See
        <a href="https://doc.rust-lang.org/unstable-book/index.html">The Unstable Book</a>
        for features implemented in <code>rustc</code>
      </td>
  </tr>
</tbody>
</table>

```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)