Skip to content

Commit 156a319

Browse files
authored
Merge branch 'master' into catch_panic_anti_pattern
2 parents cb6ad8f + ff9298a commit 156a319

29 files changed

+295
-69
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ on:
66
pull_request:
77

88
jobs:
9-
10-
test:
9+
deploy-test:
1110
runs-on: ubuntu-20.04
1211
steps:
1312
- uses: actions/checkout@v2
@@ -24,3 +23,21 @@ jobs:
2423
mdbook-version: '${{ steps.mdbook-version.outputs.MDBOOK_VERSION }}'
2524

2625
- run: mdbook build
26+
27+
doc-test:
28+
runs-on: ubuntu-20.04
29+
steps:
30+
- uses: actions/checkout@v2
31+
32+
- name: Read .env
33+
id: mdbook-version
34+
run: |
35+
. ./.env
36+
echo "::set-output name=MDBOOK_VERSION::${MDBOOK_VERSION}"
37+
38+
- name: Setup mdBook
39+
uses: peaceiris/actions-mdbook@v1
40+
with:
41+
mdbook-version: '${{ steps.mdbook-version.outputs.MDBOOK_VERSION }}'
42+
43+
- run: mdbook test

CONTRIBUTING.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Contributing
2+
3+
## Discssion board
4+
5+
If you have a question or an idea regarding certain content but you want to have feedback of fellow community members
6+
and you think it may not be appropriate to file an issue open a discussion in our [discussion board](https://github.com/rust-unofficial/patterns/discussions).
7+
8+
9+
## Writing a new article
10+
11+
Before writing a new article please check our [issues](https://github.com/rust-unofficial/patterns/issues) and
12+
the [Pull Requests](https://github.com/rust-unofficial/patterns/pulls) if there are existing issues or someone
13+
is working on that topic.
14+
15+
If you don't find an issue regarding your topic and you are sure it is not more feasible to open a thread in the [discussion board](https://github.com/rust-unofficial/patterns/discussions)
16+
please open a new issue, so we can discuss about the ideas and future content of the article together and maybe
17+
give some feedback/input on it.
18+
19+
When writing a new article it's recommended to copy the [pattern template](https://github.com/rust-unofficial/patterns/blob/master/template.md) into the
20+
appropriate directory and start editing it. You may not want to fill out every section and remove it or you might want to add extra sections.
21+
22+
Consider writing your article in a way that has a low barrier of entry so also [Rustlings](https://github.com/rust-lang/rustlings) can follow
23+
and understand the thought process behind it. So we can encourage people to use these patterns early on.
24+
25+
We encourage you to write idiomatic Rust code that builds in the [playground](https://play.rust-lang.org/).
26+
27+
If you use links to blogposts or in general content that is not to be sure existing in a few years (e.g. pdfs) please take a snapshot
28+
with the [Wayback Machine](https://web.archive.org/) and use the link to that snapshot in your article.
29+
30+
Don't forget to add your new article to the `SUMMARY.md` to let it be rendered to the book.
31+
32+
Please make `Draft Pull requests` early so we can follow your progress and can give early feedback (see the following section).
33+
34+
35+
36+
## Creating a Pull Request
37+
38+
"Release early and often!" also applies to pull requests!
39+
40+
Once your article has some visible work, create a `[WIP]` draft pull request and give it a description of what you did or want to do.
41+
Early reviews of the community are not meant as an offense but to give feedback.
42+
43+
A good principle: "Work together, share ideas, teach others."
44+
45+
### Important Note
46+
47+
Please **don't force push** your branch to keep commit history and make it easier of us to see changes between reviews.
48+
49+
Make sure to `Allow edits of maintainers` (under the text box) in the PR so people can actually collaborate on things or fix smaller issues themselves.

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# Rust Design Patterns
22

3-
An open source repository of design patterns and idioms in the Rust programming
4-
language.
3+
An open source book about design patterns and idioms in the Rust programming
4+
language that you can read [here](https://rust-unofficial.github.io/patterns/).
55

66

77
## Contents
88

99
[Introduction](intro.md)
1010

11+
1112
### Idioms
1213

1314
* [Constructor](idioms/ctor.md)
1415
* [Concatenating strings with `format!`](idioms/concat-format.md)
1516
* [Privacy for extensibility](idioms/priv-extend.md)
1617
* TODO stability for extensibility
17-
* TODO trait to separate visibility of methods from visibility of data (https://github.com/sfackler/rust-postgres/blob/master/src/lib.rs#L1400)
18+
* TODO trait to separate visibility of methods from visibility of data (https://github.com/sfackler/rust-postgres/blob/v0.9.6/src/lib.rs#L1400)
1819
* [Collections are smart pointers](idioms/deref.md)
1920
* TODO leak amplification ("Vec::drain sets the Vec's len to 0 prematurely so that mem::forgetting Drain "only" mem::forgets more stuff. instead of exposing uninitialized memory or having to update the len on every iteration")
2021
* [Finalisation in destructors](idioms/dtor-finally.md)
@@ -28,6 +29,7 @@ language.
2829
* TODO FFI usage (By being mindful of how to provide Rust libraries, and make use of existing libraries across the FFI, you can get more out of benefits Rust can bring)
2930
* [Easy doc initialization](idioms/rustdoc-init.md)
3031

32+
3133
### Design patterns
3234

3335
* [Builder](patterns/builder.md)
@@ -53,7 +55,6 @@ language.
5355
* [Compose structs together for better borrowing](patterns/compose-structs.md)
5456

5557

56-
5758
### Anti-patterns
5859

5960
* [catch_unwind for exceptions](anti_patterns/catch_panic.md)
@@ -68,16 +69,11 @@ language.
6869

6970
## Contributing
7071

71-
Contributions are very welcome!
72-
73-
You should start with [the template](template.md). Copy it into the appropriate
74-
directory, edit it, and submit a PR. You might not want every section, and you
75-
might want to add extra sections.
72+
You are missing content in this repository that can be helpful for others and you are eager to explain it?
73+
Awesome! We are always happy about new contributions (e.g. elaboration or corrections on certain topics) to this project.
7674

77-
We suggest leaving a comment on the [issue tracker](https://github.com/rust-unofficial/patterns/issues)
78-
so that other people don't start working on the same topic.
75+
We suggest reading our [Contribution guide](./CONTRIBUTING.md) to get more information on how it works.
7976

80-
Correction and elaboration PRs are very welcome.
8177

8278
## Building with mdbook
8379

@@ -93,5 +89,8 @@ If you want to build it locally you can run one of these two commands in the roo
9389

9490
Serves the book at `http://localhost:3000` (port is changeable, take a look at the terminal output
9591
to be sure) and reloads the browser when a change occurs.
96-
97-
92+
93+
94+
## License
95+
96+
This content of this repository is licensed under **MPL-2.0**; see [LICENSE](./LICENSE).

SUMMARY.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Summary
22

33
- [Introduction](./intro.md)
4+
45
- [Idioms](./idioms/README.md)
56
- [Concatenating Strings with `format!`](./idioms/concat-format.md)
67
- [Constructor](./idioms/ctor.md)
@@ -18,7 +19,6 @@
1819
- [Design Patterns](./patterns/README.md)
1920
- [Builder](./patterns/builder.md)
2021
- [Compose Structs](./patterns/compose-structs.md)
21-
- [Entry API](./patterns/entry.md)
2222
- [Fold](./patterns/fold.md)
2323
- [Late Bound Bounds](./patterns/late-bounds.md)
2424
- [Newtype](./patterns/newtype.md)
@@ -31,3 +31,7 @@
3131
- [catch_unwind for exceptions](./anti_patterns/catch_panic.md)
3232
- [`#[deny(warnings)]`](./anti_patterns/deny-warnings.md)
3333
- [Deref Polymorphism](./anti_patterns/deref.md)
34+
35+
- [Functional Programming](./functional/README.md)
36+
37+
- [Additional Resources](./additional_resources.md)

additional_resources.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Additional resources
2+
3+
A collection of complementary helpful content
4+
5+
## Talks
6+
7+
- [Design Patterns in Rust](https://www.youtube.com/watch?v=Pm_oO0N5B9k) by Nick Cameron at the PDRust (2016)

anti_patterns/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Anti-patterns
22

3-
TODO: add description/explanation
3+
An [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern) is a solution to a "recurring problem that is usually ineffective and risks being highly counterproductive".
4+
Just as valuable as knowing how to solve a problem, is knowing how _not_ to solve it.
5+
Anti-patterns give us great counter-examples to consider relative to design patterns.
6+
Anti-patterns are not confined to code. For example, a process can be an anti-pattern, too.

anti_patterns/deny-warnings.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ All this conspires to potentially break the build whenever something changes.
3636

3737
Furthermore, crates that supply additional lints (e.g. [rust-clippy]) can no
3838
longer be used unless the annotation is removed. This is mitigated with
39-
[--cap-lints].
39+
[--cap-lints]. The `--cap-lints=warn` command line argument, turns all `deny`
40+
lint errors into warnings. But be aware that `forbid` lints are stronger than
41+
`deny` hence the 'forbid' level cannot be overridden to be anything lower than
42+
an error. As a result `forbid` lints will still stop compilation.
4043

4144
## Alternatives
4245

@@ -45,7 +48,7 @@ setting from the code, and second, we can name the lints we want to deny
4548
explicitly.
4649

4750
The following command line will build with all warnings set to `deny`:
48-
51+
4952
```RUSTFLAGS="-D warnings" cargo build```
5053

5154
This can be done by any individual developer (or be set in a CI tool like
@@ -55,7 +58,7 @@ without requiring a change to the code.
5558
Alternatively, we can specify the lints that we want to `deny` in the code.
5659
Here is a list of warning lints that is (hopefully) safe to deny:
5760

58-
```rust
61+
```rust,ignore
5962
#[deny(bad-style,
6063
const-err,
6164
dead-code,
@@ -84,7 +87,7 @@ Here is a list of warning lints that is (hopefully) safe to deny:
8487

8588
In addition, the following `allow`ed lints may be a good idea to `deny`:
8689

87-
```rust
90+
```rust,ignore
8891
#[deny(missing-debug-implementations,
8992
missing-docs,
9093
trivial-casts,

anti_patterns/deref.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ public static void main(String[] args) {
2626

2727
We can use the deref polymorphism anti-pattern to do so:
2828

29-
```rust
29+
```rust,ignore
30+
use std::ops::Deref;
31+
3032
struct Foo {}
3133
3234
impl Foo {
33-
fn m(&self) { ... }
35+
fn m(&self) {
36+
//..
37+
}
38+
3439
}
3540
3641
struct Bar {
@@ -68,7 +73,7 @@ well as `Bar`.
6873

6974
You save a little boilerplate, e.g.,
7075

71-
```rust
76+
```rust,ignore
7277
impl Bar {
7378
fn m(&self) {
7479
self.f.m()

functional/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Functional Usage of Rust
2+
3+
Rust is an imperative language, but it follows many functional programming paradigms. One of the biggest hurdles to understanding functional programs when coming from an imperative background is the shift in thinking. Imperative programs describe __how__ to do something, whereas declarative programs describe __what__ to do. Let's sum the numbers from 1 to 10 to show this.
4+
5+
## Imperative
6+
7+
```rust
8+
let mut sum = 0;
9+
for i in 1..11 {
10+
sum += i;
11+
}
12+
println!("{}", sum);
13+
```
14+
15+
With imperative programs, we have to play compiler to see what is happening. Here, we start with a `sum` of `0`. Next, we iterate through the range from 1 to 10. Each time through the loop, we add the corresponding value in the range. Then we print it out.
16+
17+
| `i` | `sum` |
18+
| --- | ----- |
19+
| 1 | 1 |
20+
| 2 | 3 |
21+
| 3 | 6 |
22+
| 4 | 10 |
23+
| 5 | 15 |
24+
| 6 | 21 |
25+
| 7 | 28 |
26+
| 8 | 36 |
27+
| 9 | 45 |
28+
| 10 | 55 |
29+
30+
This is how most of us start out programming. We learn that a program is a set of steps.
31+
32+
## Declarative
33+
34+
```rust
35+
println!("{}", (1..11).fold(0, |a, b| a + b));
36+
```
37+
38+
Whoa! This is really different! What's going on here? Remember that with declarative programs we are describing __what__ to do, rather than __how__ to do it. `fold` is a function that [composes](https://en.wikipedia.org/wiki/Function_composition) functions. The name is a convention from Haskell.
39+
40+
Here, we are composing functions of addition (this closure: `|a, b| a + b)`) with a range from 1 to 10. The `0` is the starting point, so `a` is `0` at first. `b` is the first element of the range, `1`. `0 + 1 = 1` is the result. So now we `fold` again, with `a = 1`, `b = 2` and so `1 + 2 = 3` is the next result. This process continues until we get to the last element in the range, `10`.
41+
42+
| `a` | `b` | result |
43+
| --- | --- | ------ |
44+
| 0 | 1 | 1 |
45+
| 1 | 2 | 3 |
46+
| 3 | 3 | 6 |
47+
| 6 | 4 | 10 |
48+
| 10 | 5 | 15 |
49+
| 15 | 6 | 21 |
50+
| 21 | 7 | 28 |
51+
| 28 | 8 | 36 |
52+
| 36 | 9 | 45 |
53+
| 45 | 10 | 55 |

idioms/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# Idioms
22

3-
TODO: add description/explanation
3+
[Idioms](https://en.wikipedia.org/wiki/Programming_idiom) are commonly used styles and patterns largely agreed upon by a community. They are guidelines. Writing idiomatic code allows other developers to understand what is happening because they are familiar with the form that it has.
4+
5+
The computer understands the machine code that is generated by the compiler. The language is therefore mostly beneficial to the developer. So, since we have this abstraction layer, why not put it to good use and make it simple?
6+
7+
Remember the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle): "Keep It Simple, Stupid". It claims that "most systems work best if they are kept simple rather than made complicated; therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided".
8+
9+
> Code is there for humans, not computers, to understand.

0 commit comments

Comments
 (0)