Struct core::ops::RangeTo

1.0.0 · source ·
pub struct RangeTo<Idx> {
    pub end: Idx,
}
Expand description

范围仅排在 (..end) 之上。

RangeTo ..end 包含 x < end 的所有值。 它不能用作 Iterator,因为它没有起点。

Examples

..end 语法是 RangeTo

assert_eq!((..5), std::ops::RangeTo { end: 5 });
Run

它没有 IntoIterator 实现,因此不能直接在 for 循环中使用它。 这不会编译:

// error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
// std::Iterator` 不满足
for i in ..5 {
    // ...
}
Run

当用作 切片索引 时,RangeTo 会在 end 所指示的索引之前生成所有数组元素的切片。

let arr = [0, 1, 2, 3, 4];
assert_eq!(arr[ ..  ], [0, 1, 2, 3, 4]);
assert_eq!(arr[ .. 3], [0, 1, 2      ]); // 这是 `RangeTo`
assert_eq!(arr[ ..=3], [0, 1, 2, 3   ]);
assert_eq!(arr[1..  ], [   1, 2, 3, 4]);
assert_eq!(arr[1.. 3], [   1, 2      ]);
assert_eq!(arr[1..=3], [   1, 2, 3   ]);
Run

Fields§

§end: Idx

范围 (exclusive) 的上限。

Implementations§

source§

impl<Idx: PartialOrd<Idx>> RangeTo<Idx>

1.35.0 · source

pub fn contains<U>(&self, item: &U) -> boolwhere Idx: PartialOrd<U>, U: ?Sized + PartialOrd<Idx>,

如果范围中包含 item,则返回 true

Examples
assert!( (..5).contains(&-1_000_000_000));
assert!( (..5).contains(&4));
assert!(!(..5).contains(&5));

assert!( (..1.0).contains(&0.5));
assert!(!(..1.0).contains(&f32::NAN));
assert!(!(..f32::NAN).contains(&0.5));
Run

Trait Implementations§

source§

impl<Idx: Clone> Clone for RangeTo<Idx>

source§

fn clone(&self) -> RangeTo<Idx>

返回值的副本。 Read more
source§

fn clone_from(&mut self, source: &Self)

source 执行复制分配。 Read more
source§

impl<Idx: Debug> Debug for RangeTo<Idx>

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

使用给定的格式化程序格式化该值。 Read more
source§

impl<Idx: Hash> Hash for RangeTo<Idx>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

将该值输入给定的 HasherRead more
1.3.0 · source§

fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)where Self: Sized,

将这种类型的切片送入给定的 Hasher 中。 Read more
source§

impl<Idx: PartialEq> PartialEq<RangeTo<Idx>> for RangeTo<Idx>

source§

fn eq(&self, other: &RangeTo<Idx>) -> bool

此方法测试 selfother 值是否相等,并由 == 使用。
source§

fn ne(&self, other: &Rhs) -> bool

此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
1.28.0 · source§

impl<T> RangeBounds<T> for RangeTo<&T>

source§

fn start_bound(&self) -> Bound<&T>

开始索引绑定。 Read more
source§

fn end_bound(&self) -> Bound<&T>

结束索引绑定。 Read more
1.35.0 · source§

fn contains<U>(&self, item: &U) -> boolwhere T: PartialOrd<U>, U: ?Sized + PartialOrd<T>,

如果范围中包含 item,则返回 trueRead more
1.28.0 · source§

impl<T> RangeBounds<T> for RangeTo<T>

source§

fn start_bound(&self) -> Bound<&T>

开始索引绑定。 Read more
source§

fn end_bound(&self) -> Bound<&T>

结束索引绑定。 Read more
1.35.0 · source§

fn contains<U>(&self, item: &U) -> boolwhere T: PartialOrd<U>, U: ?Sized + PartialOrd<T>,

如果范围中包含 item,则返回 trueRead more
1.15.0 (const: unstable) · source§

impl<T> SliceIndex<[T]> for RangeTo<usize>

§

type Output = [T]

方法返回的输出类型。
const: unstable · source§

fn get(self, slice: &[T]) -> Option<&[T]>

🔬This is a nightly-only experimental API. (slice_index_methods)
如果在边界内,则返回此位置输出的共享引用。
const: unstable · source§

fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]>

🔬This is a nightly-only experimental API. (slice_index_methods)
如果在边界内,则对此位置的输出返回一个可变引用。
const: unstable · source§

unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T]

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的共享引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]
const: unstable · source§

unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T]

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的变量引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]
const: unstable · source§

fn index(self, slice: &[T]) -> &[T]

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的共享引用,如果越界则会触发 panic。
const: unstable · source§

fn index_mut(self, slice: &mut [T]) -> &mut [T]

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的变量引用,如果越界则会触发 panic。
1.20.0 (const: unstable) · source§

impl SliceIndex<str> for RangeTo<usize>

使用语法 &self[.. end]&mut self[.. end] 实现子字符串切片。

从字节范围 [0, end) 中返回给定字符串的切片。 等效于 &self[0 .. end]&mut self[0 .. end]

此运算为 O(1)。

在 1.20.0 之前,IndexIndexMut 的直接实现仍支持这些索引操作。

Panics

如果 end 没有指向字符的起始字节偏移量 (由 is_char_boundary 定义),或者 end > len,就会出现 panics。

§

type Output = str

方法返回的输出类型。
const: unstable · source§

fn get(self, slice: &str) -> Option<&Self::Output>

🔬This is a nightly-only experimental API. (slice_index_methods)
如果在边界内,则返回此位置输出的共享引用。
const: unstable · source§

fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output>

🔬This is a nightly-only experimental API. (slice_index_methods)
如果在边界内,则对此位置的输出返回一个可变引用。
const: unstable · source§

unsafe fn get_unchecked(self, slice: *const str) -> *const Self::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的共享引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]
const: unstable · source§

unsafe fn get_unchecked_mut(self, slice: *mut str) -> *mut Self::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的变量引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]
const: unstable · source§

fn index(self, slice: &str) -> &Self::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的共享引用,如果越界则会触发 panic。
const: unstable · source§

fn index_mut(self, slice: &mut str) -> &mut Self::Output

🔬This is a nightly-only experimental API. (slice_index_methods)
返回此位置输出的变量引用,如果越界则会触发 panic。
source§

impl<Idx: Copy> Copy for RangeTo<Idx>

source§

impl<Idx: Eq> Eq for RangeTo<Idx>

source§

impl<T> OneSidedRange<T> for RangeTo<T>where Self: RangeBounds<T>,

source§

impl<Idx> StructuralEq for RangeTo<Idx>

source§

impl<Idx> StructuralPartialEq for RangeTo<Idx>

Auto Trait Implementations§

§

impl<Idx> RefUnwindSafe for RangeTo<Idx>where Idx: RefUnwindSafe,

§

impl<Idx> Send for RangeTo<Idx>where Idx: Send,

§

impl<Idx> Sync for RangeTo<Idx>where Idx: Sync,

§

impl<Idx> Unpin for RangeTo<Idx>where Idx: Unpin,

§

impl<Idx> UnwindSafe for RangeTo<Idx>where Idx: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

从拥有的值中一成不变地借用。 Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

发生转换错误时返回的类型。
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

发生转换错误时返回的类型。
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。