-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Syntax for slices #13
Comments
But more easily confused with the normal index operator by a novice, no? Especially when you consider the kinds of error messages that the compiler will emit when someone accidentally omits the index argument to the index operator?
(Anyway, I'm not actually sure I'm in favor of this, but I'll have to think on it further. Then again, there may be some interesting ternary operators one could abuse this syntax to express; whether that's a pro or a con is subjective, of course...) |
(This will even happen automatically with auto-deref if I understand it, right?) |
I am in favour of writing Having slice assignation would be nice, also: Python's approach to slicing as a whole is quite interesting also in that Python also allows for tuples and (My comments here evidently encompass more than just a simple slice syntax. They're probably unreasonably broad in scope for now, but hopefully food for thought and an alternative model worth considering.) |
I would personally like to have an option to change the index type, assuming something more like the proposal of @nikomatsakis goes through (as opposed to that of @chris-morgan). That is, trait Slice<T, Idx=uint> {
fn as_slice<'a>(&'a self) -> &'a [T];
fn slice_from<'a>(&'a self, from: &Idx) -> &'a [T];
fn slice_to<'a>(&'a self, to: &Idx) -> &'a [T];
fn slice<'a>(&'a self, from: &Idx, to: &Idx) -> &'a [T];
} That said, if/when this gets approved in some manner, I'd like to take a crack at adding it as my first non-trivial contribution; I added it around two months ago without too much work, but didn't end up sending a PR due to being indecisive over the return type. |
-1 I see this as superfluous. |
Does this have any bearing on the eventual fate of autoslicing, since the current incarnations of |
Autoslicing should be possible (I think? I'm not totally sure if it'll autoderef to match a function argument) with impl<T> Deref<[T]> for Vec {
fn deref<'a>(&'a self) -> &'a [T] {
self.as_slice()
}
} I'm not sure if that's a good idea, both because of the ownership issues you raised and the fact that methods on |
I'm very slightly in favor of slicing syntax, but I think it would have to be compatible with strings too, not just vectors. |
@kballard: Isn't the intend that this would work with any type implementing the I personally like this idea, it seems like useful syntax sugar for a common operation. Like others I too prefer |
@Florob The proposed return value of the slice operation is |
+1 for parameterising the index type - I wish Vec had this too, interesting sugar, but with the move to Vec, Box it almost seems strange to me that rust is keeping a slice concept built into the language. Perhaps i'm perceiving that wrong, perhaps it is a lower level concept, closer to raw arrays. |
Vec, StrBuf are for owned containers. But most functions take slices or &str. Having convenient syntax for that is quite nice. |
The (However, discussion of the appropriateness of having the |
Given that strings already return |
@P1start yes, string |
@P1start Returning a Given that, if we support |
@chris-morgan +1, numerical/matrix stuff is a pretty big use case for indexing and slicing. In particular, assuming that we have your "anonymous union types" in some future, writing in a natural indexing syntax:
would be substantially more pleasant than using tuple indices:
I like the A possible solution:
I think using
(edited 8/5) A |
+1 from me, with |
Closed in favor of #198 |
Boost focus on testing
English Mother@#$%*&^ Do You Speak It?
add noop function reference
There is a long discussion on rust-lang/rust#4160 discussing the possibility of adding explicit slice operators to the language. Clearly if this is to be done, it requires an RFC and explicit discussion and approval.
I think the consensus on that thread was for the proposal found in this comment (full disclosure, by me):
Add a
Slice
trait:Add a slice operator
expr[a..b]
wherea
andb
are optional.expr[..] => expr.as_slice()
expr[a..] => expr.slice_from(a)
expr[..b] => expr.slice_to(b)
expr[a..b] => expr.slice(a, b)
Do we have something for mutable slices? Perhaps
expr[mut a..b]
? These come up less frequently but nonetheless it seems useful, particularly for fixed-length vectors since otherwise there remains no explicit syntax for slicing one that does not rely on coercion.I'm still roughly in favor of this, though I think I would change the syntax
expr[..]
toexpr[]
, just because it's shorter. Note though that with DST one can simply do&*vec
to get theas_slice()
notation.The text was updated successfully, but these errors were encountered: