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

Clarification: Use Slice instead of Array #14102

Merged
merged 1 commit into from
May 11, 2014
Merged

Clarification: Use Slice instead of Array #14102

merged 1 commit into from
May 11, 2014

Conversation

moonglum
Copy link
Contributor

There are no arrays in Rust, they are slices. Especially in the tutorial beginners should not be confused with wrong terminology. It helps to know the right names for things when you want to find something in the documentation.

@erickt explained that today to me and it helped me a lot when getting started 😉 Maybe we should also explain what a slice and what a vector is in the tutorial. If you like that, I will try to do that and attach that to the pull request 😉

@lilyball
Copy link
Contributor

There actually are arrays. They're fixed-size. Sometimes called fixed-sized vectors, sometimes called fixed-sized arrays, and sometimes just called arrays. I think standardizing on the final term is appropriate. This is a type of the form [T, ..N], e.g. [int, ..10].

@@ -1678,7 +1678,7 @@ contained value are destroyed.
~~~
use std::rc::Rc;

// A fixed-size array allocated in a reference-counted box
// A slice allocated in a reference-counted box
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately this one and the Gc one are actually legitimately fixed-size arrays (the internal bit has type [int, .. 10]).

@lilyball
Copy link
Contributor

An array is a contiguous sequence of values with a statically-known length. [int, ..10] is an array of length 10 with element type int.

A slice is &[T], which is a reference to a contiguous sequence of values with a length that is only known at runtime. The compiler has no idea what the length is (it could be 0, it could be 5318008). Also, by virtue of being a reference, it's not an owned value but is instead borrowing some other owned value.

An array will implicitly coerce into a slice as necessary (e.g. because you're passing it to a method that requires a slice).

@thestinger
Copy link
Contributor

The preferred terms are the following:

  • array ([T, ..n]), an unboxed block of memory
  • slice ([T] in general once dynamically sized types exists, only &[T] and &mut [T] for now), a pointer and length referring to a block of memory (&[T] is a view, &mut [T] is a mutable view)
  • vector (Vec<T>), a dynamic array (pointer, length, and capacity)

@moonglum
Copy link
Contributor Author

Thank you so much for all your feedback 👍

@moonglum
Copy link
Contributor Author

Adjusted everything to your comments. Mind to check again?

@kballard Ok, I think I'm not ready yet to commit code to Rust itself 😉 But I will look into print_all and see if I can change it to take a Vec<T> instead of a ~[T].

@lilyball
Copy link
Contributor

@moonglum print_all() isn't a function in Rust. It's just part of the code block that's in the documentation right above the text you changed.

@moonglum
Copy link
Contributor Author

Thanks for explaining those three concepts. Puh! I have to let that sink in I guess 😉

@@ -2428,9 +2428,9 @@ fn draw_all<T: Drawable>(shapes: ~[T]) {
# draw_all(~[c]);
~~~~

You can call that on an array of circles, or an array of rectangles
You can call that on a slice of circles, or a slice of rectangles
Copy link
Contributor

Choose a reason for hiding this comment

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

Both "slice"s in this line should be "vector"s.

Especially in the tutorial beginners should not be confused with
wrong terminology. It helps to know the right names for things
when you want to find something in the documentation.
@moonglum
Copy link
Contributor Author

Ok, I adjusted the print_all method.

@lilyball
Copy link
Contributor

There's still code in the tutorial that uses ~[T]. That's why I was saying that perhaps that change shouldn't be done as part of this PR, because you've now left it in a somewhat inconsistent state.

My suggestion is to drop the print_all change and just stick with the wording change. We can revisit the ~[T] -> Vec<T> change later.

@moonglum
Copy link
Contributor Author

Ok, removed that commit from this pull request. Will do a separate one and hunt for all the cases in the tutorial.

@moonglum
Copy link
Contributor Author

Thanks for your patience with my tiny, tiny pull request 👍

bors added a commit that referenced this pull request May 11, 2014
…=kballard

There are no arrays in Rust, they are slices. Especially in the tutorial beginners should not be confused with wrong terminology. It helps to know the right names for things when you want to find something in the documentation.

@erickt explained that today to me and it helped me a lot when getting started 😉 Maybe we should also explain what a slice and what a vector is in the tutorial. If you like that, I will try to do that and attach that to the pull request 😉
@bors bors closed this May 11, 2014
@bors bors merged commit 1895ad2 into rust-lang:master May 11, 2014
@moonglum
Copy link
Contributor Author

👯

@moonglum moonglum deleted the slice-clarification-in-readme branch May 12, 2014 17:15
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 13, 2023
flip1995 pushed a commit to flip1995/rust that referenced this pull request Feb 20, 2025
Labeled blocks cannot be used as-is in the "then" or "else" part of an
`if` expression. They must be enclosed in an anonymous block.

Fix rust-lang#14099

changelog: [`match_bool`]: fix suggestion when the rewritten block has a
label
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.

5 participants