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

Iterators #395

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ personal_ws-1.1 en 0 utf-8
abcabcabc
abcd
abcdefghijklmnopqrstuvwxyz
adaptor
adaptors
Addr
aliasability
alignof
Expand All @@ -22,6 +24,7 @@ bitwise
Bitwise
bitxor
BitXor
Bjarne
Boehm
bool
boolean
Expand Down Expand Up @@ -84,7 +87,10 @@ filename
Filename
filesystem
Filesystem
FnMut
FnOnce
formatter
FromIterator
GitHub
gitignore
grapheme
Expand All @@ -111,6 +117,7 @@ indices
init
instantiation
internet
IntoIterator
InvalidDigit
ioerror
iokind
Expand All @@ -121,6 +128,7 @@ IpAddrKind
irst
isize
iter
iterator's
JavaScript
lang
latin
Expand Down Expand Up @@ -214,6 +222,7 @@ Stdin
stdlib
stdout
steveklabnik's
Stroustrup
struct
Struct
structs
Expand Down
6 changes: 5 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@

## Thinking in Rust

- [Functional Language Features in Rust - Iterators and Closures](ch13-00-functional-features.md)
- [Functional Language Features in Rust](ch13-00-functional-features.md)
- [Closures](ch13-01-closures.md)
- [Iterators](ch13-02-iterators.md)
- [Improving our I/O Project](ch13-03-improving-our-io-project.md)
- [Performance](ch13-04-performance.md)

- [More about Cargo and Crates.io](ch14-00-more-about-cargo.md)
- [Release Profiles](ch14-01-release-profiles.md)
Expand Down
10 changes: 9 additions & 1 deletion src/ch12-04-testing-the-librarys-functionality.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ fn grep<'a>(search: &str, contents: &'a str) -> Vec<&'a str> {
Finally, we need a way to store the lines that contain our search string. For
that, we can make a mutable vector before the `for` loop and call the `push`
method to store a `line` in the vector. After the `for` loop, we return the
vector:
vector. Listing 12-15 has the full implementation:

<figure>
<span class="filename">Filename: src/lib.rs</span>

```rust
Expand All @@ -206,6 +207,13 @@ fn grep<'a>(search: &str, contents: &'a str) -> Vec<&'a str> {
}
```

<figcaption>

Listing 12-15: Fully functioning implementation of the `grep` function

</figcaption>
</figure>

<!-- Will add ghosting and wingdings in libreoffice /Carol -->

Let's give it a try:
Expand Down
11 changes: 10 additions & 1 deletion src/ch12-05-working-with-environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ Trust me.";

We're going to define a new function named `grep_case_insensitive`. Its
implementation will be almost the same as the `grep` function, but with some
minor changes:
minor changes as shown in Listing 12-16:

<figure>
<span class="filename">Filename: src/lib.rs</span>

```rust
Expand All @@ -71,6 +72,14 @@ fn grep_case_insensitive<'a>(search: &str, contents: &'a str) -> Vec<&'a str> {
}
```

<figcaption>

Listing 12-16: Implementing a `grep_case_insensitive` function by changing the
search string and the lines of the contents to lowercase before comparing them

</figcaption>
</figure>

<!-- Will add ghosting and wingdings in libreoffice /Carol -->

First, we lowercase the `search` string, and store it in a shadowed variable
Expand Down
4 changes: 2 additions & 2 deletions src/ch12-06-writing-to-stderr-instead-of-stdout.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Problem parsing arguments: not enough arguments

We'd like this to be printed to the screen instead, and only have the output
from a successful run end up in the file if we run our program this way. Let's
change how error messages are printed as shown in Listing 12-15:
change how error messages are printed as shown in Listing 12-17:

<figure>
<span class="filename">Filename: src/main.rs</span>
Expand Down Expand Up @@ -67,7 +67,7 @@ fn main() {

<figcaption>

Listing 12-15: Writing error messages to `stderr` instead of `stdout`
Listing 12-17: Writing error messages to `stderr` instead of `stdout`

</figcaption>
</figure>
Expand Down
56 changes: 19 additions & 37 deletions src/ch13-00-functional-features.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
# Functional Language features in Rust - Iterators and Closures

## Closures

### What is a closure

How are they diff from fns

### `Fn` traits

## Iterators

### Iterator & for loop

.into_iter()

### Iterators are Lazy

Difference between adapter and consumer - another iterator or consuming?

### Implementing the Iterator trait

Talk about using Associated Types here, foreshadow to advanced type systems
chapter about why this is a different thing than normal

## ??? How does this improve the I/O project from Chapter 12

Does this get woven into the above sections?

## Summary: Performance

### Iterators compile down to ASM == for loop

Most complicated chain of iterator functions that compile down to the same ASM as a for loop

### Representation: Closures are a Struct

Closures don't have any further performance penalty over regular fn calls

Rust's design has taken inspiration from a lot of previous work. One of Rust's
influences is functional programming, where functions are values that can be
used as arguments or return values to other functions, assigned to variables,
and so forth. We're going to sidestep the issue of what, exactly, functional
programming is or is not, and instead show off some features of Rust that
are similar to features in many languages referred to as functional.

More specifically, we're going to cover:

* *Closures*, a function-like construct you can store in a variable.
* *Iterators*, a way of processing series of elements.
* How to use these features to improve upon the project from the last chapter.
* The performance of these features. Spoiler alert: they're faster than you
might think!

This is not a complete list of Rust's influence from the functional style:
pattern matching, enums, and many other features are too. But mastering
closures and iterators are an important part of writing idiomatic, fast Rust
code.
Loading