-
Notifications
You must be signed in to change notification settings - Fork 13.3k
mentioned difference between memory allocation of arrays and vectors in book #20982
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
@@ -60,6 +59,16 @@ let v = vec![1, 2, 3]; // v: Vec<i32> | |||
brackets `[]` with `vec!`. Rust allows you to use either in either situation, | |||
this is just convention.) | |||
|
|||
Vectors are to arrays what `String` is to `&str`. While the arrays are |
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 isn't correct. There's three core array-ish types:
[T; n]
: Array[T]
: SliceVec<T>
: Vector
[T; n]
is "on the stack", because it has a compile-time-constant size.
[T]
is a dynamically-sized type because it is a handle to some compile-time-unknown number of elements. In that sense, [T]
can't ever exist on its own; it must exist behind a pointer as e.g. Box<[T]>
or &[T]
. Any such pointer becomes "fat" as it includes the number of elements in the slice as run-time information in addition to the actual address where the slice starts. A slice may just as easily refer to the contents of a Vec or Array.
Vec is a heap-allocated growable array, conceptually similar to a Box<[T]>
, but without the requirement that capacity = length.
Vec == String
, and &str == &[T]
, not [T; n]
. There is no string analog to [T; n]
.
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.
Whoops, total brain fart. My brain read "slice". Sigh. You are totally correct.
@gankro Thanks for the feedback! I myself am learning rust, and your explanation clears up some things for me! It's clear that the line you've commented on is misleading and needs to be rewritten. I should probably add some of that awesome explanation as another commit! 👍 Also, while we are at it, @steveklabnik: while I was reading the book for the first time it felt like there were multiple "kinds" of slices (string slice I think the "slice part" of the strings doc needs more explanation about how it's backed by arrays (or we should change the order of arrays and strings doc). What do you think? |
ping to involved parties |
@steveklabnik: please take a look at my last comment and give me your suggestions... |
Shortly, the full Strings guide should be back as an intermediate chapter, and it explains this. |
Since this will need re-written anyway, I'm going to give this version a close. The |
Fix for #20948
Mentioned the difference between how arrays and vectors are allocated on memory. Please review the changes.