Skip to content

Introduce 'Idiomatic Rust' learning module #2800

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

LukeMathWalker
Copy link
Contributor

This PR introduces:

  • A new section for the "Idiomatic Rust" learning module
  • (The beginning of) the section on newtype patterns


# Leveraging the Type System

Rust's type system is _expressive_.\
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a style thing: I don't think we forced line breaks anywhere else in the course. I don't think most people know about the Markdown syntax either.

Personally, I don't like such line breaks because I don't want to dictate the formatting — CSS will do that and I only want to deal with the semantics (paragraphs, lists, tables).

Also, I generally like a very terse style for slides:

One line introduction to the topic

- Some key point
- Another key point

I know we don't follow this very closely, but that Variables still show this style.

Basically, I would try to avoid paragraphs, especially with multiple sentences per paragraph.

But I'm fine to merge this now and then we'll copy-edit it much more going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the reference!
Do you think edfe9e8 is closer to the style we're looking for?

We can use types and traits to build abstractions that make our code harder to
misuse. In some cases, we can even go as far as enforcing correctness at
_compile-time_. Quite often, these abstractions have no runtime
overhead[^zero-cost].
Copy link
Collaborator

Choose a reason for hiding this comment

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

Foot notes are difficult to use on slides: they break the flow if the reader has to move from the text here to the bottom and back up.

I would avoid the footnote and instead explain the concept in the speaker notes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Converted into a speaker note in edfe9e8

- Rust's type system borrows a lot of ideas from functional programming
languages.\
For example, Rust's enums are known as "algebraic data types" in languages
like Haskell and OCaml. You can take inspiration from learning material geared
Copy link
Collaborator

Choose a reason for hiding this comment

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

I love this extra background, very useful!

If you happen to have a list of such helpful resources, then feel free to expand https://google.github.io/comprehensive-rust/other-resources.html -- perhaps with a new section on the type system!

Also, https://google.github.io/comprehensive-rust/glossary.html is an important page to know about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the threshold for inclusion in "Other resources"? Right now, all resources mentioned are Rust-focused and relatively "bulky" in size.
I expect we'll have a lot of references to articles—would those be suitable inclusions?


# Newtype Pattern

A _newtype_ is a wrapper around an existing type, often a primitive:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would like a throw-back to the other page that introduces the newtype pattern (perhaps in the speaker notes, depending on how this looks on screen).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added bidirectional links in 51d050e.

Unlike type aliases, newtypes aren't interchangeable with the wrapped type:

```rust,compile_fail
# pub struct UserId(u64);
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have so far avoided the # construct: it make the Playground read-only and so the instructor cannot do any live coding (or engage with questions).

Also, the slide very quickly becomes too big. This is about as much space that you have:

image

The compiler error immediately ends up outside of the page:

image

I would suggest taking away the "This doesn't compile ❌" comments — let the instructor show, not tell.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The screenshots come from the artifact bundle uploaded for this PR test run: https://github.com/google/comprehensive-rust/actions/runs/16028068797 -- all the way at the bottom the page, you find zip files for each language (useful for me if I don't want to grab the branch and do mdbook build myself).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still adjusting to the slide format 😁 But "Show, don't tell" is a great framing for thinking about the medium, thanks.

Re: size: Even if we remove unnecessary comments, we'll struggle to fit compilation errors in the viewport.
The code example in "Parse, Don't Validate" is relatively minimal, but it'll fill the screen if everything is shown (no #).

Is the issue with the Playground flow related to the "Copy" button not including the hidden code?

Copy link
Collaborator

@djmitche djmitche left a comment

Choose a reason for hiding this comment

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

It would be helpful to see an overall outline for the new course.

  • This would facilitate high-level review of the course content before you spend the time to write the slides
  • This would help us think about how long this course might last (is it just a morning course?)
  • This would provide an opportunity for others to contribute sections or slides
  • It would give us all more time to think about good exercises - I've found this to be the most time-consuming part of a section

target_minutes: 180
---

# Welcome to Idiomatic Rust
Copy link
Collaborator

Choose a reason for hiding this comment

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

This new course should be mentioned in src/running-the-course/course-structure.md as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the pointer!
I've added it in 447904a, although I had to change the course plugin slightly to handle multi-word course names.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, good fix, thanks!

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.

3 participants