-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
some work on collections #137
Conversation
So yeah, as someone who wants to know more about how to use things like Vectors in my code, but not really care so much about how they work under the hood, this isn't my favorite approach ever. BUT! Knowing what I know now, since I've acquired some rust experience, I think by adding a little bit of cheese to the broccoli you could get past-me to eat my vegetables and I would be better off for it. I knew about the double-capacity reallocation behavior of
And one small thought:
|
@carols10cents, perhaps "common collections," differentiating on how often collections are used in real-world programs. |
Yeah, I think this might be real great!
👍 And yeah, "common" or "staple" both sound good to me. |
Yay! Almost forgot that I made some tiny edits while reading, I just pushed them to this branch for you. I'm going to add an issue to remind me to make that function and flame graphs soon-- I'll let you take care of the intro, unless you'd like me to give it a try! |
v.push(8); | ||
``` | ||
|
||
Since these numbers are `i32`s, Rust can infer the type of the vector, so we |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are they always i32 for simple numbers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an integer which doesn't have any bounds to infer the type by is resolved as i32, yes.
👍 to @carols10cents comments. Carol's knee-jerk about show me how to use Vec vs telling me how it works. I think we should spend a lot more time on how to use it, and then move the "let's open the hood and eat our broccoli" to later in the book (or possibly to a blog post or other venue). There may be ways to show some of that data, maybe not quite all of the steps you show. When I was looking through the chapter, I was thinking that I'd much rather see pretty pictures. Maybe that's a good cheese for the broccoli where you can get some of the vitamins in. |
On the call, @aturon asked @steveklabnik about a case in which knowing the internals of |
f9aa969
to
3d90c48
Compare
Okay, @carols10cents , I think a draft is done. The hashmap bit is.... uninspired? We should try to brainstorm on how to jazz it up a bit. But the basics are all in place, I think. |
I was talking with @ashleygwilliams about what stuff to add to the hashmap section:
|
also: where are the values? are they copied, referenced? how does equality work? |
c854a11
to
0a6d904
Compare
['न', 'म', 'स', '्', 'त', 'े', ' '] | ||
``` | ||
|
||
There are seven of them, and the last one isn't even visible! Finally, if you |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@steveklabnik I'm not really sure what you're trying to illustrate with the space on the end... the way you talk about it here ("the last [char] isn't even visible!", "and there's still that empty character on the end") makes it sound like something weirder than a regular ol' space? I think it's distracting from the main grapheme cluster/character point, mind if I take it out?
|
||
However. | ||
|
||
Sometimes, indexing the bytes of a string is useful. So while you can't use `[]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This explanation is... unsatisfying. Like... I understand the pieces of this decision, namely:
- People naïvely try to get chars by indexing a string and then make their programs not do what they expected in the face of multibyte characters, and Rust wants to try to prevent that
- string slices are a starting byte and a number of bytes indexed into a String
but like, together, with the explanation "this thing is bad but this other thing that amounts to a trivial workaround is useful so we disallowed the bad thing but allowed the useful trivial workaround" is not very... satisfying? convincing that the language has a cohesive design?
There might not be a satisfying explanation here, and Rust just might be inconsistent here and everyone has come to terms with it. In that case, I feel like we should say "Yes, Rust is being inconsistent because the problems that occur with indexing directly into an array were deemed to outweigh the benefits of allowing it, but that did not hold for slicing because... X".
I am going to go looking for discussions along these lines, I bet there's a real humdinger of a bikeshed around here somewhere that I don't remember seeing.
Ok. I think I'm done with this chapter. Anyone have time to review in the next few days? |
- [Vectors]() | ||
- [Strings]() | ||
- [`HashMap<K, V>`]() | ||
- [Essential Collections](ch08-00-essential-collections.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh i know we already talked about this, but I would really prefer something other than "essential." How about "fundamental"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YOU DID THIS 57fd1a5
(but yeah fundamental is fine)
- [Essential Collections](ch08-00-essential-collections.md) | ||
- [Vectors](ch08-01-vectors.md) | ||
- [Strings](ch08-02-strings.md) | ||
- [HashMaps](ch08-03-hashmaps.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two words, probably.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hnnnggg like everywhere then
@@ -0,0 +1,18 @@ | |||
# Essential Collections |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll need to change this here too
|
||
Before we can dig into those aspects, we need to talk about what exactly we | ||
even mean by the word 'string'. Rust actually only has one string type in the | ||
core language itself: `&str`. We talked about these string slices in Chapter 4: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth *ing string slices?
// ... etc | ||
``` | ||
|
||
There are crates available to get grapheme clusters from `String`s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add 'on crates.io'?
@@ -0,0 +1,251 @@ | |||
## HashMaps | |||
|
|||
The last of our essential collections is the *HashMap*. A `HashMap<K, V>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
darn essences
### Updating a HashMap | ||
|
||
#### Overwriting a Value | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
two headings with no words inside?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops. i can put some words in there
|
||
#### Overwriting a Value | ||
|
||
If we insert a key and a value, then insert that key with a different value, the value associated with that key will be replaced. Even though this code calls `insert` twice, the HashMap will only contain one key/value pair, since we're inserting with the key `1` both times: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
word wrap this line :)
This chapter is so much better now 😄 ❤️ Okay, so one last thing that we didn't include that might be cool: should we show off how to use enums to get multiple things into a collection? |
Oooooh good call. I'm into it. |
i'm not totally sure. I guess it's like "vector" vs On Mon, Sep 26, 2016 at 5:53 PM, Carol (Nichols || Goulding) <
|
No more essences. And "hash map" looked really weird at first but I think I'm getting used to it now. Back over to you! 🏓 |
the vector would hold a type that would cause errors with the operations we | ||
performed on the vector. Using an enum plus a `match` where we access elements | ||
in a vector like this means that Rust will ensure at compile time that we | ||
always handle every possible case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last thing, and then let's
We should mention "if we always know what the set of types are, an enum works. if we don't, a trait object works, and we'll learn about those later" (but in better words)
check me!! |
🎊 |
yayayayayayyyyy!!!! |
Using an enum for storing different types in a vector does imply that we need | ||
to know the set of types we'll want to store at compile time. If that's not the | ||
case, instead of an enum, we can use a trait object. We'll learn about those in | ||
Chapter XX. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this meant to be Chapter 25.2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've been rearranging the later chapters a lot, so we're not sure which chapter it's going to be yet.
same way as `println!`, but instead of printing the output to the screen, it | ||
returns a `String` with the contents. This version is much easier to read than | ||
all of the `+`s. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this mention how format!
doesn't take ownership (unlike the prior examples)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe! Can you elaborate on why mentioning that would be helpful to you at this point? Which prior examples are you referring to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the first example of concatenation there was an aside about String + &str
taking ownership of the first part (which might be an extra point against doing the multiple +
given potential precedence oddities) but there's nothing about how format!
takes &
(I went and looked it up in the docs/code
basically it's because ownership is brought up during the previous example but not during this it just felt a little strange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I'll think on this. I think I was assuming that since we said that format!
is like println!
here, and the reader has already had some experience with println!
, that it should be clear. But apparently it's not! I'm also going to create a new bug report since this PR has been merged already, so this doesn't get lost. Thank you!! ❤️
mdBook 0.4.5へアップグレード
…ary-page rust-lang#134 add glossary page
I started fleshing out some stuff about data structures. I feel pretty good about this, with some caveats discussed below.
Also, I would really like to hear what @carols10cents, @aturon, and @jonathandturner (think about showing the internal representation of
Vec
like this; we haven't seen raw pointers, but I really like the idea of giving people an understanding at this level. I'm not sure if this pacing is correct, like, maybe the details of howinsert()
works should go at the end, rather than in the middle?There are two ways this PR needs more work, beyond all that: