Struct std::ops::RangeTo
范围仅排在 (..end) 之上。
RangeTo ..end 包含 x < end 的所有值。 它不能用作 Iterator,因为它没有起点。
pub struct RangeTo<Idx> {
pub end: Idx,
} RangeTo的语法是..end
assert_eq!((..5), std::ops::RangeTo { end: 5 });它没有 IntoIterator 实现,因此不能直接在 for 循环中使用它。 这不会编译:
// error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
// std::Iterator` 不满足
for i in ..5 {
// ...
}当用作 切片索引时,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 ]);Fields
end
范围 (exclusive) 的上限(不包含)。
end: IdxImplementations
impl<Idx> RangeTo<Idx>
impl<Idx> RangeTo<Idx>
where
Idx: PartialOrd<Idx>,contains
如果范围中包含 item,则返回 true。
pub fn contains<U>(&self, item: &U) -> bool
where
Idx: PartialOrd<U>,
U: PartialOrd<Idx> + ?Sized,示例
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));Trait Implementations
impl Clone for RangeTo
clone
返回值的副本。
fn clone(&self) -> RangeToclone_from
从 source执行复制分配。
fn clone_from(&mut self, source: &Self)impl Debug for RangeTo
fmt
使用给定的格式化程序格式化该值。
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>impl Hash for RangeTo
impl<Idx> Hash for RangeTo
where
Idx: Hash,hash
将该值输入给定的 Hasher。
fn hash<__H>(&self, state: &mut __H)
where
__H: Hasher,hash_slice
将这种类型的切片送入给定的 Hasher 中。
fn hash_slice<H>(data: &[Self], state: &mut H)
where
H: Hasher,
Self: Sized,impl Index<RangeTo> for CString
Output
索引后返回的类型。
type Output = OsStrindex
执行索引container[index]操作。
fn index(&self, _index: RangeTo) -> &OsStrimpl Index<RangeTo> for OsString
Output
索引后返回的类型。
type Output = OsStrindex
执行索引 (container[index]) 操作。
fn index(&self, _index: RangeTo) -> &OsStrimpl Index<RangeTo> for String
Output
索引后返回的类型。
type Output = strindex
执行索引 (container[index]) 操作。
fn index(&self, _index: RangeTo) -> &strimpl IndexMut<RangeTo> for OsString
index_mut
执行可变索引 (container[index]) 操作。
fn index_mut(&mut self, _index: RangeTo) -> &mut OsStrimpl IndexMut<RangeTo> for String
index_mut
执行可变索引 (container[index]) 操作。
fn index_mut(&mut self, _index: RangeTo) -> &mut strimpl PartialEq<RangeTo> for RangeTo<Idx>
eq
此方法测试 self 和 other 值是否相等,并由 == 使用。
fn eq(&self, other: &RangeTo) -> boolne
此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
fn ne(&self, other: &Rhs) -> boolimpl<T> RangeBounds<T> for RangeTo<T>
impl<T> RangeBounds<T> for RangeTo
where
T: ?Sized,start_bound
开始索引绑定。
fn start_bound(&self) -> Bound<&T>end_bound
结束索引绑定。
fn end_bound(&self) -> Bound<&T>contains
如果范围中包含 item,则返回 true。
fn contains<U>(&self, item: &U) -> bool
where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,impl<T> RangeBounds<T> for RangeTo<&T>
impl<T> RangeBounds<T> for RangeTo
where
T: ?Sized,start_bound
开始索引绑定。
fn start_bound(&self) -> Bound<&T>end_bound
结束索引绑定。
fn end_bound(&self) -> Bound<&T>contains
如果范围中包含 item,则返回 true。
fn contains<U>(&self, item: &U) -> bool
where
T: PartialOrd<U>,
U: PartialOrd<T> + ?Sized,impl<T> SliceIndex<[T]> for RangeTo<usize>
Output
方法返回的输出类型。
type Output = [T]get
nightly-only 如果在边界内,则返回此位置输出的共享引用。
fn get(self, slice: &[T]) -> Option<&[T]>get_mut
nightly-only 如果在边界内,则对此位置的输出返回一个可变引用。
fn get_mut(self, slice: &mut [T]) -> Option<&mut [T]>get_unchecked
nightly-only 返回此位置输出的共享引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]。
unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T]get_unchecked_mut
nightly-only 返回此位置输出的变量引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]。
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut [T]index
nightly-only 返回此位置输出的共享引用,如果越界则会触发 panic。
fn index(self, slice: &[T]) -> &[T]index_mut
nightly-only 返回此位置输出的变量引用,如果越界则会触发 panic。
fn index_mut(self, slice: &mut [T]) -> &mut [T]impl SliceIndex<str> for RangeTo<usize>
使用语法 &self[begin .. end] 或 &mut self[begin .. end] 实现子字符串切片。
从字节范围 [begin,end) 返回给定字符串的切片。
此运算为 O(1)。
在 1.20.0 之前,Index 和 IndexMut 的直接实现仍支持这些索引操作。
Panics
如果 begin 或 end 未指向字符的起始字节偏移量 (由 is_char_boundary 定义),begin > end 或 end > len,就会出现 panics。
Examples
let s = "Löwe 老虎 Léopard";
assert_eq!(&s[0 .. 1], "L");
assert_eq!(&s[1 .. 9], "öwe 老");
// 这些将是 panic:
// 字节 2 位于 `ö` 内:
// &s[2 ..3];
// byte 8 lies within `老` &s[1 ..
// 8];
// 字节 100 在字符串 &s[3 之外。
// 100];方法返回的输出类型。
type Output = strget
nightly-only 如果在边界内,则返回此位置输出的共享引用。
fn get(self, slice: &str) -> Option<&<RangeTo<usize> as SliceIndex<str>>::Output>get_mut
nightly-only 如果在边界内,则对此位置的输出返回一个可变引用。
fn get_mut(
self,
slice: &mut str
) -> Option<&mut <RangeTo<usize> as SliceIndex<str>>::Output>get_unchecked
nightly-only 返回此位置输出的共享引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]。
unsafe fn get_unchecked(
self,
slice: *const str
) -> *const <RangeTo<usize> as SliceIndex<str>>::Outputget_unchecked_mut
nightly-only 返回此位置输出的变量引用,而不执行任何边界检查。 即使未使用所得的引用,使用越界索引或悬垂的 slice 指针调用此方法也是 [undefined 行为]。
unsafe fn get_unchecked_mut(
self,
slice: *mut str
) -> *mut <RangeTo<usize> as SliceIndex<str>>::Outputindex
nightly-only 返回此位置输出的共享引用,如果越界则会触发 panic。
fn index(self, slice: &str) -> &<RangeTo<usize> as SliceIndex<str>>::Outputindex_mut
nightly-only 返回此位置输出的变量引用,如果越界则会触发 panic。
fn index_mut(
self,
slice: &mut str
) -> &mut <RangeTo<usize> as SliceIndex<str>>::Outputimpl Copy for RangeTo
impl Eq for RangeTo
impl StructuralEq for RangeTo
impl StruturalPartialEq for RangeTo
Auto Trait Implementations
impl RefUnwindSafe for RangeTo
impl Send for RangeTo
impl Sync for RangeTo
impl Unpin for RangeTo
impl UnwindSafe for RangeTo
Blanket Implementations
impl<T> Any for T
impl<T> Any for T
where
T: 'static + ?Sized,impl<T> Borrow<T> for T
impl<T> Borrow<T> for T
where
T: ?Sized,impl<T> BorrowMut<T> for T
impl<T> BorrowMut<T> for T
where
T: ?Sized,impl<T> From<T> for T
impl<T> ToOwned for T
impl<T> ToOwned for T
where
T: Clone,impl<T, U> TryFrom<U> for T
impl<T, U> TryFrom<U> for T
where
U: Into<T>,impl<T, U> TryInto<U> for T
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,