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

Guide: develop the exposition of arrays, vectors, and slices. #17962

Merged
merged 1 commit into from
Oct 15, 2014

Conversation

jkleint
Copy link

@jkleint jkleint commented Oct 12, 2014

The array is the fundamental concept; vectors are growable arrays, and
slices are views into either. Show common array ops up front: length
and iteration. Mention arrays are immutable by default. Highlight
definite initialization and bounds-checking as safety features. Show
that you only need a type suffix on one element of initializers.
Explain that vectors are a value-add library type over arrays, not a
fundamental type; show they have the same "interface." Motivate slices
as efficient views into arrays; explain you can slice vectors, Strings,
&str because they're backed by arrays. Show off new, easy-to-read
[a..b] slice syntax.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pcwalton (or someone else) soon.

brackets (`[]`) with `vec!`. Rust allows you to use either in either situation,
this is just convention.
Because arrays always have a fixed length, the length is actually part of the
type: an array holding `N` items of type `T` has type `[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.

I'm slightly concerned about this notation here, because we have yet to use generics, so it doesn't really mean anything. It's one of the reasons I didn't write it in the original version.

Copy link
Author

Choose a reason for hiding this comment

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

It was mentioned:

Rust has different types to represent this idea: Vec<T> (a 'vector'), [T, .. N] (an 'array'), and &[T] (a 'slice').

I'm assuming you mean you didn't mention what T and N are? I wasn't thinking generics, just a pseudo-high-school-algebra point of view: these are "variables" that stand for something. I can take that bit out if you feel it's too difficult.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, just we haven't explained this notation whatsoever at this point.

@steveklabnik
Copy link
Member

Thank you! I think overall this is an improvement, I just have a few inline comments. Also, one space after a period, please.

@jkleint
Copy link
Author

jkleint commented Oct 12, 2014

OK, made the changes you suggested. Thanks for the guidance, and let me know if there's anything else you'd like to fix.

Similar to `&str`, a slice is a reference to another array. We can get a
slice from a vector by using the `as_slice()` method:
A **vector** is a dynamic or "growable" array, implemented as the standard
library type [`Vec<T>`](http://doc.rust-lang.org/std/vec/). Vectors are to
Copy link
Member

Choose a reason for hiding this comment

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

This should be a relative link, just std/vec.

And probably should have another brief "remember, we'll talk about what the <T> means later" bit here too.

@steveklabnik
Copy link
Member

Just a few last nits. This is really improved overall. Thanks for the quick turnaround! Editing is the worst part of the writing process 😄

@jkleint
Copy link
Author

jkleint commented Oct 12, 2014

No problem, here you go.

is `names[0]` and the second name is `names[1]`. The above example prints
`The second name is: Brian`. If you try to use a subscript that is not in the
array, you will get an error: array access is bounds-checked at run-time. Such
errant access is the source of many a bug in other systems programming
Copy link
Contributor

Choose a reason for hiding this comment

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

s/a bug/bugs

@steveklabnik
Copy link
Member

r=me after that last typo, thank you @parir !

@jkleint
Copy link
Author

jkleint commented Oct 13, 2014

Cool, thanks again for all the help, and for a great Guide.

The array is the fundamental concept; vectors are growable arrays, and
slices are views into either.  Show common array ops up front: length
and iteration.  Mention arrays are immutable by default.  Highlight
definite initialization and bounds-checking as safety features.  Show
that you only need a type suffix on one element of initializers.
Explain that vectors are a value-add library type over arrays, not a
fundamental type; show they have the same "interface." Motivate slices
as efficient views into arrays; explain you can slice vectors, Strings,
&str because they're backed by arrays.
@jkleint
Copy link
Author

jkleint commented Oct 15, 2014

OK, fixed a missing type suffix; sorry about that. I can't run the doc tests because of #17220 I think. Try this.

bors added a commit that referenced this pull request Oct 15, 2014
The array is the fundamental concept; vectors are growable arrays, and
slices are views into either.  Show common array ops up front: length
and iteration.  Mention arrays are immutable by default.  Highlight
definite initialization and bounds-checking as safety features.  Show
that you only need a type suffix on one element of initializers.
Explain that vectors are a value-add library type over arrays, not a
fundamental type; show they have the same "interface." Motivate slices
as efficient views into arrays; explain you can slice vectors, Strings,
&str because they're backed by arrays.  Show off new, easy-to-read
[a..b] slice syntax.
@bors bors closed this Oct 15, 2014
@bors bors merged commit 1ce5a56 into rust-lang:master Oct 15, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Aug 29, 2024
fix: Fix Return Type Syntax to include `..` (i.e. `method(..)` and not `method()`) as specified in the RFC

Fixes rust-lang#17952.
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.

6 participants