Skip to content

Commit 593ca58

Browse files
committed
draft: cargo-teach
1 parent 2ac6042 commit 593ca58

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: Converting Rustfix to cargo teach
3+
category:
4+
- rust
5+
---
6+
7+
## User story examples
8+
9+
### Binary tree
10+
11+
Inspired by [this post][mre-tree] by [Matthias Endler][mre].
12+
13+
```rust
14+
struct Tree {
15+
root: i64,
16+
left: Tree,
17+
right: Tree,
18+
}
19+
```
20+
21+
```sh
22+
cargo check
23+
```
24+
25+
```
26+
error[E0072]: recursive type `Tree` has infinite size
27+
--> src/main.rs:1:1
28+
|
29+
1 | struct Tree {
30+
| ^^^^^^^^^^^ recursive type has infinite size
31+
|
32+
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Tree` representable
33+
```
34+
35+
```sh
36+
cargo teach
37+
```
38+
39+
```
40+
==> Hi there! Let me have a look at that nice `$CRATE_NAME` code!
41+
> ...done!
42+
> This looks pretty good! You'll need to tweak a few things to make it work, though.
43+
> Let's get started!
44+
45+
==> Have a look at `src/main.rs`:
46+
|
47+
1 | struct Tree {
48+
2 | root: i64,
49+
3 | left: Tree,
50+
4 | right: Tree,
51+
5 | }
52+
|
53+
54+
==> Sadly, this doesn't work, because: Recursive type `Tree` has infinite size.
55+
> When defining a recursive struct or enum, any use of the type being defined
56+
> from inside the definition must occur behind a pointer (like `Box` or `&`).
57+
> This is because structs and enums must have a well-defined size, and without
58+
> the pointer, the size of the type would need to be unbounded.
59+
60+
( ) I don't get it. Explain some more?
61+
(x) I see! What should I do?
62+
63+
==> Thanks for asking!
64+
> I'd suggest you insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Tree` representable
65+
```
66+
67+
This uses:
68+
69+
- Quoted code showing the whole item (not just the header here)
70+
- Error messages, transformed to work in sentence
71+
- First paragraph of `rustc --explain E0072`
72+
- Suggestion
73+
74+
[mre]: https://matthias-endler.de/
75+
[mre-tree]: https://matthias-endler.de/2017/boxes-and-trees/

0 commit comments

Comments
 (0)