Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

bharadwaj6
Copy link

Fix for #20948

Mentioned the difference between how arrays and vectors are allocated on memory. Please review the changes.

@rust-highfive
Copy link
Contributor

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.

@bharadwaj6 bharadwaj6 changed the title mentioned difference between memory allocation of arrays and vectors in book #20948 mentioned difference between memory allocation of arrays and vectors in book Jan 12, 2015
@@ -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
Copy link
Contributor

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]: Slice
  • Vec<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].

Copy link
Member

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.

@bharadwaj6
Copy link
Author

@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 &str, array slice), but while reading arrays section (thanks to the line that says: A slice is a "view" into an array) it was getting clear what a slice really was.

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?

@Gankra
Copy link
Contributor

Gankra commented Jan 21, 2015

ping to involved parties

@bharadwaj6
Copy link
Author

@steveklabnik: please take a look at my last comment and give me your suggestions...

@steveklabnik
Copy link
Member

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?

Shortly, the full Strings guide should be back as an intermediate chapter, and it explains this.

@steveklabnik
Copy link
Member

Since this will need re-written anyway, I'm going to give this version a close. The Strings chapter covers this kind of information, and so I think that it should be expanded if there's still a misunderstanding. 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.

4 participants