Skip to content

'Contains' method for ranges #1434

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

Merged
merged 2 commits into from
Mar 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions text/0000-contains-method-for-ranges.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
- Feature Name: contains_method
- Start Date: 2015-12-28
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary
[summary]: #summary

Implement a method, `contains()`, for `Range`, `RangeFrom`, and `RangeTo`, checking if a number is in the range.

Note that the alternatives are just as important as the main proposal.

# Motivation
[motivation]: #motivation

The motivation behind this is simple: To be able to write simpler and more expressive code. This RFC introduces a "syntactic sugar" without doing so.

# Detailed design
[design]: #detailed-design

Implement a method, `contains()`, for `Range`, `RangeFrom`, and `RangeTo`. This method will check if a number is bound by the range. It will yield a boolean based on the condition defined by the range.

The implementation is as follows (placed in libcore, and reexported by libstd):

```rust
use core::ops::{Range, RangeTo, RangeFrom};

impl<Idx> Range<Idx> where Idx: PartialOrd<Idx> {
fn contains(&self, item: Idx) -> bool {
self.start <= item && self.end > item
}
}

impl<Idx> RangeTo<Idx> where Idx: PartialOrd<Idx> {
fn contains(&self, item: Idx) -> bool {
self.end > item
}
}

impl<Idx> RangeFrom<Idx> where Idx: PartialOrd<Idx> {
fn contains(&self, item: Idx) -> bool {
self.start <= item
}
}

```

# Drawbacks
[drawbacks]: #drawbacks

Lacks of generics (see Alternatives).

# Alternatives
[alternatives]: #alternatives

## Add a `Contains` trait

This trait provides the method `.contains()` and implements it for all the Range types.

## Add a `.contains<I: PartialEq<Self::Item>>(i: I)` iterator method

This method returns a boolean, telling if the iterator contains the item given as parameter. Using method specialization, this can achieve the same performance as the method suggested in this RFC.

This is more flexible, and provide better performance (due to specialization) than just passing a closure comparing the items to a `any()` method.

## Make `.any()` generic over a new trait

Call this trait, `ItemPattern<Item>`. This trait is implemented for `Item` and `FnMut(Item) -> bool`. This is, in a sense, similar to `std::str::pattern::Pattern`.

Then let `.any()` generic over this trait (`T: ItemPattern<Self::Item>`) to allow `any()` taking `Self::Item` searching through the iterator for this particular value.

This will not achieve the same performance as the other proposals.

# Unresolved questions
[unresolved]: #unresolved-questions

None.