Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite part of the tutorial #10690

Merged
merged 1 commit into from Dec 5, 2013
Merged

rewrite part of the tutorial #10690

merged 1 commit into from Dec 5, 2013

Conversation

thestinger
Copy link
Contributor

This begins a rewrite of some sections the tutorial as an introduction
to concepts through the implementation of a simple data structure. I
think this would be a good way to introduce references, generics, traits
and many other concepts too. For example, the section introducing
alternatives to ownership can demonstrate a persistent list.

@thestinger
Copy link
Contributor Author

This can be expanded by going into detail about how to mutate the list in-place by using a mutable reference, including an introduction to swaps. It can then introduce named lifetimes by demonstrating searching through the list and returning a reference to the value. It could introduce traits by implementing Iterator, with a for loop thrown in as a bonus. The list can be turned into a generic type, too.

I think this is a much better way to teach the language than how we're doing it right now.

~~~ {.xfail-test}
// error: illegal recursive enum type; wrap the inner value in a box to make it representable
enum List {
Cons(int, List),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth not using int? (because of e.g. #9940)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Also, it might be "smoother" to mention that we're going to be storing ints in the text.)

@emberian
Copy link
Member

This looks really good to me.

@alexcrichton
Copy link
Member

I like the idea of the tutorial waking through implementing a structure or a small project, but it seems a little out of place as written. There's lots of general "these are rust concepts" sections, and then all of a sudden there's a section on implementing a linked list. As @huonw pointed out, the linked-list idea also just kinda falls out of scope suddenly without much closure in what just happened.

I have a feeling that @brson may want to weigh in on this, but I would be wary on adding such a sudden interjection of implementing a linked list without much of an intro or conclusion.

@thestinger
Copy link
Contributor Author

I think the current design of a section dedicated to every major language feature with isolated examples is material for the manual rather than a tutorial. It falls out of scope suddenly because I haven't dedicated very much time to it yet as I'm not sure it will be accepted. I do plan on extending it to be many times larger than it is at the moment and covering generics, iterators, references and other features this way.

My view of what a tutorial should be is a concise overview of the language and then jumping right in to implementing something simple like a linked list. It can then continue on with more complex examples like an AI for a simple board game. It should cover all the high-level semantics of each language feature this way but it doesn't need to get bogged down in the details as we have a manual.

@pcwalton
Copy link
Contributor

pcwalton commented Dec 4, 2013

@brson Opinions?


An *owned box* (`~`) uses a dynamic memory allocation to provide the invariant
of always being the size of a pointer, regardless of the contained type. This
can be leverage to create a valid `List` definition:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though the motivation is good, this seems like a fairly round-about way to describe boxes. It sort of claims that boxes exist to create types that are pointer sized, which is a fairly novel way to think about boxes. It also is a bit of a tautology, paraphrasing "boxes use allocation to be the size of a pointer", since "box" and "pointer" are essentially synonyms, this is saying "boxes use allocation and are the size of boxes".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the first mention of "pointers"?

@brson
Copy link
Contributor

brson commented Dec 4, 2013

I agree with this direction, but also agree with others that this style needs to be carried over throughout the tutorial. What are your plans to expand this?

@brson
Copy link
Contributor

brson commented Dec 4, 2013

Well, I see that you outlined some of your plans earlier. Let me think a sec.

> ***Note:*** The `Option` type is an enum representing an *optional* value.
> It's comparable to a nullable pointer in many other languages, but stores the
> contained value unboxed.
assert_eq!(std::mem::size_of::<Foo>(), std::mem::size_of::<u32>() * 4);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the std::mem:: is obscuring these examples, they'd be clearer as

use std::mem::size_of; // import the size_of function

assert_eq!(size_of::<Foo>(), size_of::<u32>() * 4)

...

I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use statements don't seem to be covered at all :(

This begins a rewrite of some sections the tutorial as an introduction
to concepts through the implementation of a simple data structure. I
think this would be a good way to introduce references, traits and many
other concepts too. For example, the section introducing alternatives to
ownership can demonstrate a persistent list.
bors added a commit that referenced this pull request Dec 5, 2013
This begins a rewrite of some sections the tutorial as an introduction
to concepts through the implementation of a simple data structure. I
think this would be a good way to introduce references, generics, traits
and many other concepts too. For example, the section introducing
alternatives to ownership can demonstrate a persistent list.
@bors bors closed this Dec 5, 2013
@bors bors merged commit c1eb20b into rust-lang:master Dec 5, 2013
@thestinger thestinger deleted the doc branch December 7, 2013 00:10
~~~

This error message is related to Rust's precise control over memory layout, and
solving it will require introducing the concept of *boxing*.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this example. This is the exact issue I ran into when I first played around with Rust. Having this as part of the tutorial is certainly going to help a lot of people.

flip1995 pushed a commit to flip1995/rust that referenced this pull request May 5, 2023
…ts, r=llogiq

Document that `cargo clippy --fix` implies `--all-targets`

In [`cargo fix`'s documentation](https://doc.rust-lang.org/cargo/commands/cargo-fix.html) they indicate that `fix` implies `--all-targets` if no target is supplied. As Clippy uses Cargo under the hood, this also applies to Clippy, but we didn't document that behaviour.

This PR changes that

Fixes rust-lang#10690
changelog: Add to documentation that `--fix` implies `--all-targets`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants