-
Notifications
You must be signed in to change notification settings - Fork 13.4k
RFC: Python-like slices in vec #1799
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
Comments
+1 all around. Thanks, /be Niko Matsakis wrote:
|
Oops, replied to the list. For the record, +1 all around. /be |
Looks good. Why not make islice into slice? |
I agree with killerswan. Just call |
I'd make the unsigned-integer version fail on out-of-range indices. So you'd have the choice of a liberal version and a strict version. Also, yes, name one of them simply |
I'd be ok with |
As a general principle, I think that for performance reasons, there should always be a "never-fails" version of all important language primitives. The reason is that it is common for developers to want a version that can't fail, and if the language/library doesn't offer one then the developer has to write a function to do it himself. Then what happens is, the developer checks the arguments, and then the compiler or std lib does exactly the same check a second time, which is just wasteful in an inner loop. Plus, the compiler has to spend a little extra time inlining this little function everywhere. Of course, a version that fails on unexpected input is equally useful and should also be provided. IMO, then, there should be three slice functions: two fast (unsigned with fail, unsigned without fail) and one slower and more flexible (python style). But then you would have more arguing to do about naming. Mind you, I didn't catch how slices work in Rust -- I'm assuming it's like Go where a slice is merely a pointer and a length, no copying involved. If a slice actually copies the whole range then it was silly of me to worry about the speed of the range checks. |
One thing I like about python is that it has a single slice operator (albeit with optional arguments). I find an API made up of lots of very similar functions to be confusing. |
Python's slicing mechanism really is one of my favorite aspects of the language. +1 Add my voice to the chorus of those who'd love to see the It looks like Go handles this by making slices a separate type entirely: http://blog.golang.org/2011/01/go-slices-usage-and-internals.html This issue is also related to #555. |
Don't forget about Python's extended slice notation With this is possible to reverse a list doing this |
+1 for [from:to:step] syntax. |
Why not just call vec::reverse? |
vec::reverse would be equivalent to For example, take only pair indices |
There was some gesture towards supporting this long ago, but I removed it out of lack of effort. If someone wants to champion it, I'm fine with that. |
@nikomatsakis, |
+1 I'd love to see the slicing notation
Makes for very intuitive code |
Underway, but not for 0.3 and possibly not with |
withdrawing in favor of a newer proposal based on our current slices |
I take it that means this proposal: http://smallcultfollowing.com/babysteps/blog/2012/05/14/vectors/ Not sure if there's an issue for it yet. |
Meanwhile, see https://crates.io/crates/slyce. |
where did this end up? It'd be really useful if slice ranges in Rust were more forgiving, like in Python. Does that feature exist for avoiding errors like?:
|
slyce, linked above, is completely forgiving (because of the nature of its usage in the upcoming JSONPath RFC). |
I would like to modify the vector (and string, I suppose) slicing routines to be more "Python-like". This means that they are more lenient of invalid indices and they consider negative numbers to count from the right.
All in all I propose six functions:
The
i
family accepts signed integers: negative inputs are considered as counting from the right. Theu
family takes unsigned integers. All of them are tolerant of invalid indices, returning empty list rather than failing, as Python does. In my experience this is usually what you want.The text was updated successfully, but these errors were encountered: