= othervec.clone();vec[3]; .get_mut(index); .contains(needle); .iter().find(|&T| -> bool); .binary_search(x:&T); .push(3); .append(other : Vec); .clone_from(&Vec<T>); .clone_from_slice(&[T]); .copy_from_slice(&[T]); .pop(); .remove(index); .swap_remove(index); .truncate(i); .drain(range); .retain(|i| -> bool); .dedup(); .sort(); .sort_by(|&T|->Ordering); .sort_by_key(|&T|->Key); .reverse(); .into_iter(); .chunks_mut(cnk_sz); .windows(wnd_sz); .into_boxed_slice(); .as_ref(); .to_vec(); .as_mut_slice(); .reserve(100); .reserve_exact(100); .split_at_mut(mid); .splitn_mut(n, |&T| -> bool); .rsplitn_mut(_); .split_off(mid); .hash(state: Hasher)From<BinaryHeap> from() | BorrowMut borrow /_mut() | Clone clone/_from() | Hash hash/_slice() | IndexMut index/_mut() DerefMut deref/_mut() | FromIterator from_iter() | IntoIterator into_iter() | Extend extend() | PartialEq eq() ne() | PartialOrd partial_cmp() lt() le() gt() ge() | Eq | Ord cmp() | Drop drop() | Default default() | Debug (if T:Debug) fmt() | AsRef AsMut as_ref() as_mut() | From from() | Write write() write_all() flush() by_ref() .. | use std::collections::HashMap; = HashMap::with_capacity(); = other.clone(); .into_iter(); .keys(); .is_empty(); .contains_key(k:Q); .get_mut(k:&Q); .entry(key); .drain(); .insert(k,v); .remove(k:&Q); .from_iter(iter : <Item=(K,V)>); .clone_from(source);hm.hasher(b);let foo : Option = Some(T::new()); = None; .as_ref(); .as_mut(); .cloned(); .unwrap(); .expect(msg); .unwrap_or_default();mutableopt.take(); .map(|t| -> U); .map_or(default:U, |t| -> U); .map_or_else(|| default -> U, |t| -> U); .ok_or(err:E); .ok_or_else(|| err -> E); a.and(b : Option<U>); a.and_then(|| b -> Option<U>); a.or(b : Option<T>); a.or_else(|| b -> Option<T>);let foo : Result = Ok(T::new()); = Err(E::new()); .as_ref(); .as_mut(); .unwrap(); .expect(msg); .unwrap_err(); .map(|t| -> U); .map_err(|e| -> F); .ok(); .err(); a.and(b : Result<U,E>); a.and_then(|| b -> Result<U,E>); a.or(b : Result<T,E>); a.or_else(|| b -> Result<T,E>);Contribute at github.com/phaiax/rust-cheatsheet
A contiguous growable array type, written Vec<T> but pronounced 'vector.'
let mut vec = Vec::new(); vec.push(1); vec.push(2); assert_eq!(vec.len(), 2); assert_eq!(vec[0], 1); assert_eq!(vec.pop(), Some(2)); assert_eq!(vec.len(), 1); vec[0] = 7; assert_eq!(vec[0], 7); vec.extend([1, 2, 3].iter().cloned()); for x in &vec { println!("{}", x); } assert_eq!(vec, [7, 1, 2, 3]);
The vec! macro is provided to make initialization more convenient:
let mut vec = vec![1, 2, 3]; vec.push(4); assert_eq!(vec, [1, 2, 3, 4]);
It can also initialize each element of a Vec<T> with a given value:
let vec = vec![0; 5]; assert_eq!(vec, [0, 0, 0, 0, 0]);
Use a Vec<T> as an efficient stack:
let mut stack = Vec::new(); stack.push(1); stack.push(2); stack.push(3); while let Some(top) = stack.pop() { // Prints 3, 2, 1 println!("{}", top); }
The Vec type allows to access values by index, because it implements the
Index trait. An example will be more explicit:
let v = vec!(0, 2, 4, 6); println!("{}", v[1]); // it will display '2'
However be careful: if you try to access an index which isn't in the Vec, your software will panic! You cannot do this:
fn main() { let v = vec!(0, 2, 4, 6); println!("{}", v[6]); // it will panic! }let v = vec!(0, 2, 4, 6); println!("{}", v[6]); // it will panic!
In conclusion: always check if the index you want to get really exists before doing it.
A Vec can be mutable. Slices, on the other hand, are read-only objects. To get a slice, use "&". Example:
fn main() { fn read_slice(slice: &[usize]) { // ... } let v = vec!(0, 1); read_slice(&v); // ... and that's all! // you can also do it like this: let x : &[usize] = &v; }fn read_slice(slice: &[usize]) { // ... } let v = vec!(0, 1); read_slice(&v); // ... and that's all! // you can also do it like this: let x : &[usize] = &v;
In Rust, it's more common to pass slices as arguments rather than vectors when you just want to provide a read access. The same goes for String and &str.
The capacity of a vector is the amount of space allocated for any future elements that will be added onto the vector. This is not to be confused with the length of a vector, which specifies the number of actual elements within the vector. If a vector's length exceeds its capacity, its capacity will automatically be increased, but its elements will have to be reallocated.
For example, a vector with capacity 10 and length 0 would be an empty vector
with space for 10 more elements. Pushing 10 or fewer elements onto the
vector will not change its capacity or cause reallocation to occur. However,
if the vector's length is increased to 11, it will have to reallocate, which
can be slow. For this reason, it is recommended to use Vec::with_capacity
whenever possible to specify how big the vector is expected to get.
Due to its incredibly fundamental nature, Vec makes a lot of guarantees
about its design. This ensures that it's as low-overhead as possible in
the general case, and can be correctly manipulated in primitive ways
by unsafe code. Note that these guarantees refer to an unqualified Vec<T>.
If additional type parameters are added (e.g. to support custom allocators),
overriding their defaults may change the behavior.
Most fundamentally, Vec is and always will be a (pointer, capacity, length) triplet. No more, no less. The order of these fields is completely unspecified, and you should use the appropriate methods to modify these. The pointer will never be null, so this type is null-pointer-optimized.
However, the pointer may not actually point to allocated memory. In particular,
if you construct a Vec with capacity 0 via Vec::new(), vec![],
Vec::with_capacity(0), or by calling shrink_to_fit() on an empty Vec, it
will not allocate memory. Similarly, if you store zero-sized types inside
a Vec, it will not allocate space for them. Note that in this case the
Vec may not report a capacity() of 0. Vec will allocate if and only
if mem::size_of::<T>() * capacity() > 0. In general, Vec's allocation
details are subtle enough that it is strongly recommended that you only
free memory allocated by a Vec by creating a new Vec and dropping it.
If a Vec has allocated memory, then the memory it points to is on the heap
(as defined by the allocator Rust is configured to use by default), and its
pointer points to len() initialized elements in order (what you would see
if you coerced it to a slice), followed by capacity() - len() logically
uninitialized elements.
Vec will never perform a "small optimization" where elements are actually stored on the stack for two reasons:
It would make it more difficult for unsafe code to correctly manipulate a Vec. The contents of a Vec wouldn't have a stable address if it were only moved, and it would be more difficult to determine if a Vec had actually allocated memory.
It would penalize the general case, incurring an additional branch on every access.
Vec will never automatically shrink itself, even if completely empty. This
ensures no unnecessary allocations or deallocations occur. Emptying a Vec
and then filling it back up to the same len() should incur no calls to
the allocator. If you wish to free up unused memory, use shrink_to_fit.
push and insert will never (re)allocate if the reported capacity is
sufficient. push and insert will (re)allocate if len() == capacity().
That is, the reported capacity is completely accurate, and can be relied on.
It can even be used to manually free the memory allocated by a Vec if
desired. Bulk insertion methods may reallocate, even when not necessary.
Vec does not guarantee any particular growth strategy when reallocating
when full, nor when reserve is called. The current strategy is basic
and it may prove desirable to use a non-constant growth factor. Whatever
strategy is used will of course guarantee O(1) amortized push.
vec![x; n], vec![a, b, c, d], and Vec::with_capacity(n), will all
produce a Vec with exactly the requested capacity. If len() == capacity(),
(as is the case for the vec! macro), then a Vec<T> can be converted
to and from a Box<[T]> without reallocating or moving the elements.
Vec will not specifically overwrite any data that is removed from it, but also won't specifically preserve it. Its uninitialized memory is scratch space that it may use however it wants. It will generally just do whatever is most efficient or otherwise easy to implement. Do not rely on removed data to be erased for security purposes. Even if you drop a Vec, its buffer may simply be reused by another Vec. Even if you zero a Vec's memory first, that may not actually happen because the optimizer does not consider this a side-effect that must be preserved.
Vec does not currently guarantee the order in which elements are dropped (the order has changed in the past, and may change again).
fn new() -> Vec<T>Constructs a new, empty Vec<T>.
The vector will not allocate until elements are pushed onto it.
let mut vec: Vec<i32> = Vec::new();
fn with_capacity(capacity: usize) -> Vec<T>Constructs a new, empty Vec<T> with the specified capacity.
The vector will be able to hold exactly capacity elements without
reallocating. If capacity is 0, the vector will not allocate.
It is important to note that this function does not specify the length
of the returned vector, but only the capacity. (For an explanation of
the difference between length and capacity, see the main Vec<T> docs
above, 'Capacity and reallocation'.)
let mut vec = Vec::with_capacity(10); // The vector contains no items, even though it has capacity for more assert_eq!(vec.len(), 0); // These are all done without reallocating... for i in 0..10 { vec.push(i); } // ...but this may make the vector reallocate vec.push(11);
The Vec type allows to access values by index, because it implements the
Index trait. An example will be more explicit:
let v = vec!(0, 2, 4, 6); println!("{}", v[1]); // it will display '2'
However be careful: if you try to access an index which isn't in the Vec, your software will panic! You cannot do this:
fn main() { let v = vec!(0, 2, 4, 6); println!("{}", v[6]); // it will panic! }let v = vec!(0, 2, 4, 6); println!("{}", v[6]); // it will panic!
In conclusion: always check if the index you want to get really exists before doing it.
fn len(&self) -> usizeReturns the number of elements in the vector.
let a = vec![1, 2, 3]; assert_eq!(a.len(), 3);
fn is_empty(&self) -> boolReturns true if the vector contains no elements.
let mut v = Vec::new(); assert!(v.is_empty()); v.push(1); assert!(!v.is_empty());
fn first(&self) -> Option<&T>Returns the first element of a slice, or None if it is empty.
let v = [10, 40, 30]; assert_eq!(Some(&10), v.first()); let w: &[i32] = &[]; assert_eq!(None, w.first());
fn first_mut(&mut self) -> Option<&mut T>Returns a mutable pointer to the first element of a slice, or None if it is empty
fn last(&self) -> Option<&T>Returns the last element of a slice, or None if it is empty.
let v = [10, 40, 30]; assert_eq!(Some(&30), v.last()); let w: &[i32] = &[]; assert_eq!(None, w.last());
fn get(&self, index: usize) -> Option<&T>Returns the element of a slice at the given index, or None if the
index is out of bounds.
let v = [10, 40, 30]; assert_eq!(Some(&40), v.get(1)); assert_eq!(None, v.get(3));
fn get_mut(&mut self, index: usize) -> Option<&mut T>Returns a mutable reference to the element at the given index,
or None if the index is out of bounds
fn contains(&self, x: &T) -> bool where T: PartialEq<T>Returns true if the slice contains an element with the given value.
let v = [10, 40, 30]; assert!(v.contains(&30)); assert!(!v.contains(&50));
fn binary_search(&self, x: &T) -> Result<usize, usize> where T: OrdBinary search a sorted slice for a given element.
If the value is found then Ok is returned, containing the
index of the matching element; if the value is not found then
Err is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1,4].
let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; assert_eq!(s.binary_search(&13), Ok(9)); assert_eq!(s.binary_search(&4), Err(7)); assert_eq!(s.binary_search(&100), Err(13)); let r = s.binary_search(&1); assert!(match r { Ok(1...4) => true, _ => false, });
fn binary_search_by<F>(&self, f: F) -> Result<usize, usize> where F: FnMut(&T) -> OrderingBinary search a sorted slice with a comparator function.
The comparator function should implement an order consistent
with the sort order of the underlying slice, returning an
order code that indicates whether its argument is Less,
Equal or Greater the desired target.
If a matching value is found then returns Ok, containing
the index for the matched element; if no match is found then
Err is returned, containing the index where a matching
element could be inserted while maintaining sorted order.
Looks up a series of four elements. The first is found, with a
uniquely determined position; the second and third are not
found; the fourth could match any position in [1,4].
let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]; let seek = 13; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9)); let seek = 4; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7)); let seek = 100; assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13)); let seek = 1; let r = s.binary_search_by(|probe| probe.cmp(&seek)); assert!(match r { Ok(1...4) => true, _ => false, });
fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize> where B: Ord, F: FnMut(&T) -> BBinary search a sorted slice with a key extraction function.
Assumes that the slice is sorted by the key, for instance with
sort_by_key using the same key extraction function.
If a matching value is found then returns Ok, containing the
index for the matched element; if no match is found then Err
is returned, containing the index where a matching element could
be inserted while maintaining sorted order.
Looks up a series of four elements in a slice of pairs sorted by
their second elements. The first is found, with a uniquely
determined position; the second and third are not found; the
fourth could match any position in [1,4].
let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1), (1, 2), (2, 3), (4, 5), (5, 8), (3, 13), (1, 21), (2, 34), (4, 55)]; assert_eq!(s.binary_search_by_key(&13, |&(a,b)| b), Ok(9)); assert_eq!(s.binary_search_by_key(&4, |&(a,b)| b), Err(7)); assert_eq!(s.binary_search_by_key(&100, |&(a,b)| b), Err(13)); let r = s.binary_search_by_key(&1, |&(a,b)| b); assert!(match r { Ok(1...4) => true, _ => false, });
fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq<T>Returns true if needle is a suffix of the slice.
let v = [10, 40, 30]; assert!(v.ends_with(&[30])); assert!(v.ends_with(&[40, 30])); assert!(!v.ends_with(&[50])); assert!(!v.ends_with(&[50, 30]));
fn starts_with(&self, needle: &[T]) -> bool where T: PartialEq<T>Returns true if needle is a prefix of the slice.
let v = [10, 40, 30]; assert!(v.starts_with(&[10])); assert!(v.starts_with(&[10, 40])); assert!(!v.starts_with(&[50])); assert!(!v.starts_with(&[10, 50]));
fn push(&mut self, value: T)fn insert(&mut self, index: usize, element: T)Inserts an element at position index within the vector, shifting all
elements after it to the right.
Panics if index is greater than the vector's length.
let mut vec = vec![1, 2, 3]; vec.insert(1, 4); assert_eq!(vec, [1, 4, 2, 3]); vec.insert(4, 5); assert_eq!(vec, [1, 4, 2, 3, 5]);
fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=T>Extends a collection with the contents of an iterator. Read more
fn extend_from_slice(&mut self, other: &[T])Clones and appends all elements in a slice to the Vec.
Iterates over the slice other, clones each element, and then appends
it to this Vec. The other vector is traversed in-order.
Note that this function is same as extend except that it is
specialized to work with slices instead. If and when Rust gets
specialization this function will likely be deprecated (but still
available).
let mut vec = vec![1]; vec.extend_from_slice(&[2, 3, 4]); assert_eq!(vec, [1, 2, 3, 4]);
fn append(&mut self, other: &mut Vec<T>)Moves all the elements of other into Self, leaving other empty.
Panics if the number of elements in the vector overflows a usize.
let mut vec = vec![1, 2, 3]; let mut vec2 = vec![4, 5, 6]; vec.append(&mut vec2); assert_eq!(vec, [1, 2, 3, 4, 5, 6]); assert_eq!(vec2, []);
fn clone_from(&mut self, other: &Vec<T>)Performs copy-assignment from source. Read more
fn clone_from_slice(&mut self, src: &[T]) where T: CloneCopies the elements from src into self.
The length of this slice must be the same as the slice passed in.
This function will panic if the two slices have different lengths.
let mut dst = [0, 0, 0]; let src = [1, 2, 3]; dst.clone_from_slice(&src); assert!(dst == [1, 2, 3]);
fn copy_from_slice(&mut self, src: &[T]) where T: CopyCopies all elements from src into self, using a memcpy.
The length of src must be the same as self.
This function will panic if the two slices have different lengths.
let mut dst = [0, 0, 0]; let src = [1, 2, 3]; dst.copy_from_slice(&src); assert_eq!(src, dst);
fn pop(&mut self) -> Option<T>Removes the last element from a vector and returns it, or None if it
is empty.
let mut vec = vec![1, 2, 3]; assert_eq!(vec.pop(), Some(3)); assert_eq!(vec, [1, 2]);
fn remove(&mut self, index: usize) -> TRemoves and returns the element at position index within the vector,
shifting all elements after it to the left.
Panics if index is out of bounds.
let mut v = vec![1, 2, 3]; assert_eq!(v.remove(1), 2); assert_eq!(v, [1, 3]);
fn swap_remove(&mut self, index: usize) -> TRemoves an element from anywhere in the vector and return it, replacing it with the last element.
This does not preserve ordering, but is O(1).
Panics if index is out of bounds.
let mut v = vec!["foo", "bar", "baz", "qux"]; assert_eq!(v.swap_remove(1), "bar"); assert_eq!(v, ["foo", "qux", "baz"]); assert_eq!(v.swap_remove(0), "foo"); assert_eq!(v, ["baz", "qux"]);
fn truncate(&mut self, len: usize)Shorten a vector to be len elements long, dropping excess elements.
If len is greater than the vector's current length, this has no
effect.
let mut vec = vec![1, 2, 3, 4, 5]; vec.truncate(2); assert_eq!(vec, [1, 2]);
fn drain<R>(&mut self, range: R) -> Drain<T> where R: RangeArgument<usize>Create a draining iterator that removes the specified range in the vector and yields the removed items.
Note 1: The element range is removed even if the iterator is not consumed until the end.
Note 2: It is unspecified how many elements are removed from the vector,
if the Drain value is leaked.
Panics if the starting point is greater than the end point or if the end point is greater than the length of the vector.
let mut v = vec![1, 2, 3]; let u: Vec<_> = v.drain(1..).collect(); assert_eq!(v, &[1]); assert_eq!(u, &[2, 3]); // A full range clears the vector v.drain(..); assert_eq!(v, &[]);
fn clear(&mut self)Clears the vector, removing all values.
let mut v = vec![1, 2, 3]; v.clear(); assert!(v.is_empty());
fn retain<F>(&mut self, f: F) where F: FnMut(&T) -> boolRetains only the elements specified by the predicate.
In other words, remove all elements e such that f(&e) returns false.
This method operates in place and preserves the order of the retained
elements.
let mut vec = vec![1, 2, 3, 4]; vec.retain(|&x| x%2 == 0); assert_eq!(vec, [2, 4]);
fn dedup(&mut self)Removes consecutive repeated elements in the vector.
If the vector is sorted, this removes all duplicates.
let mut vec = vec![1, 2, 2, 3, 2]; vec.dedup(); assert_eq!(vec, [1, 2, 3, 2]);
fn sort(&mut self) where T: OrdThis is equivalent to self.sort_by(|a, b| a.cmp(b)).
This sort is stable and O(n log n) worst-case but allocates
approximately 2 * n where n is the length of self.
let mut v = [-5, 4, 1, -3, 2]; v.sort(); assert!(v == [-5, -3, 1, 2, 4]);
fn sort_by<F>(&mut self, compare: F) where F: FnMut(&T, &T) -> OrderingSorts the slice, in place, using compare to compare
elements.
This sort is stable and O(n log n) worst-case but allocates
approximately 2 * n, where n is the length of self.
let mut v = [5, 4, 1, 3, 2]; v.sort_by(|a, b| a.cmp(b)); assert!(v == [1, 2, 3, 4, 5]); // reverse sorting v.sort_by(|a, b| b.cmp(a)); assert!(v == [5, 4, 3, 2, 1]);
fn sort_by_key<B, F>(&mut self, f: F) where B: Ord, F: FnMut(&T) -> BSorts the slice, in place, using key to extract a key by which to
order the sort by.
This sort is stable and O(n log n) worst-case but allocates
approximately 2 * n, where n is the length of self.
let mut v = [-5i32, 4, 1, -3, 2]; v.sort_by_key(|k| k.abs()); assert!(v == [1, 2, -3, 4, -5]);
fn reverse(&mut self)Reverse the order of elements in a slice, in place.
let mut v = [1, 2, 3]; v.reverse(); assert!(v == [3, 2, 1]);
fn swap(&mut self, a: usize, b: usize)Swaps two elements in a slice.
Panics if a or b are out of bounds.
let mut v = ["a", "b", "c", "d"]; v.swap(1, 3); assert!(v == ["a", "d", "c", "b"]);
fn into_iter(self) -> IntoIter<T>Creates a consuming iterator, that is, one that moves each value out of the vector (from start to end). The vector cannot be used after calling this.
let v = vec!["a".to_string(), "b".to_string()]; for s in v.into_iter() { // s has type String, not &String println!("{}", s); }
fn chunks(&self, size: usize) -> Chunks<T>Returns an iterator over size elements of the slice at a
time. The chunks are slices and do not overlap. If size does not divide the
length of the slice, then the last chunk will not have length
size.
Panics if size is 0.
Print the slice two elements at a time (i.e. [1,2],
[3,4], [5]):
let v = &[1, 2, 3, 4, 5]; for chunk in v.chunks(2) { println!("{:?}", chunk); }
fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T>Returns an iterator over chunk_size elements of the slice at a time.
The chunks are mutable slices, and do not overlap. If chunk_size does
not divide the length of the slice, then the last chunk will not
have length chunk_size.
Panics if chunk_size is 0.
fn windows(&self, size: usize) -> Windows<T>Returns an iterator over all contiguous windows of length
size. The windows overlap. If the slice is shorter than
size, the iterator returns no values.
Panics if size is 0.
Print the adjacent pairs of a slice (i.e. [1,2], [2,3],
[3,4]):
let v = &[1, 2, 3, 4]; for win in v.windows(2) { println!("{:?}", win); }
fn into_boxed_slice(self) -> Box<[T]>Converts the vector into Box<[T]>.
Note that this will drop any excess capacity. Calling this and
converting back to a vector with into_vec() is equivalent to calling
shrink_to_fit().
fn as_mut_slice(&mut self) -> &mut [T]Extracts a mutable slice of the entire vector.
Equivalent to &mut s[..].
fn capacity(&self) -> usizeReturns the number of elements the vector can hold without reallocating.
let vec: Vec<i32> = Vec::with_capacity(10); assert_eq!(vec.capacity(), 10);
fn reserve(&mut self, additional: usize)Reserves capacity for at least additional more elements to be inserted
in the given Vec<T>. The collection may reserve more space to avoid
frequent reallocations.
Panics if the new capacity overflows usize.
let mut vec = vec![1]; vec.reserve(10); assert!(vec.capacity() >= 11);
fn reserve_exact(&mut self, additional: usize)Reserves the minimum capacity for exactly additional more elements to
be inserted in the given Vec<T>. Does nothing if the capacity is already
sufficient.
Note that the allocator may give the collection more space than it
requests. Therefore capacity can not be relied upon to be precisely
minimal. Prefer reserve if future insertions are expected.
Panics if the new capacity overflows usize.
let mut vec = vec![1]; vec.reserve_exact(10); assert!(vec.capacity() >= 11);
fn shrink_to_fit(&mut self)Shrinks the capacity of the vector as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
let mut vec = Vec::with_capacity(10); vec.extend([1, 2, 3].iter().cloned()); assert_eq!(vec.capacity(), 10); vec.shrink_to_fit(); assert!(vec.capacity() >= 3);
fn split_at(&self, mid: usize) -> (&[T], &[T])Divides one slice into two at an index.
The first will contain all indices from [0, mid) (excluding
the index mid itself) and the second will contain all
indices from [mid, len) (excluding the index len itself).
Panics if mid > len.
let v = [10, 40, 30, 20, 50]; let (v1, v2) = v.split_at(2); assert_eq!([10, 40], v1); assert_eq!([30, 20, 50], v2);
fn split_at_mut(&mut self, mid: usize) -> (&mut [T], &mut [T])Divides one &mut into two at an index.
The first will contain all indices from [0, mid) (excluding
the index mid itself) and the second will contain all
indices from [mid, len) (excluding the index len itself).
Panics if mid > len.
let mut v = [1, 2, 3, 4, 5, 6]; // scoped to restrict the lifetime of the borrows { let (left, right) = v.split_at_mut(0); assert!(left == []); assert!(right == [1, 2, 3, 4, 5, 6]); } { let (left, right) = v.split_at_mut(2); assert!(left == [1, 2]); assert!(right == [3, 4, 5, 6]); } { let (left, right) = v.split_at_mut(6); assert!(left == [1, 2, 3, 4, 5, 6]); assert!(right == []); }
fn split<F>(&self, pred: F) -> Split<T, F> where F: FnMut(&T) -> boolReturns an iterator over subslices separated by elements that match
pred. The matched element is not contained in the subslices.
Print the slice split by numbers divisible by 3 (i.e. [10, 40],
[20], [50]):
let v = [10, 40, 30, 20, 60, 50]; for group in v.split(|num| *num % 3 == 0) { println!("{:?}", group); }
fn split_mut<F>(&mut self, pred: F) -> SplitMut<T, F> where F: FnMut(&T) -> boolReturns an iterator over mutable subslices separated by elements that
match pred. The matched element is not contained in the subslices.
fn splitn<F>(&self, n: usize, pred: F) -> SplitN<T, F> where F: FnMut(&T) -> boolReturns an iterator over subslices separated by elements that match
pred, limited to returning at most n items. The matched element is
not contained in the subslices.
The last element returned, if any, will contain the remainder of the slice.
Print the slice split once by numbers divisible by 3 (i.e. [10, 40],
[20, 60, 50]):
let v = [10, 40, 30, 20, 60, 50]; for group in v.splitn(2, |num| *num % 3 == 0) { println!("{:?}", group); }
fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<T, F> where F: FnMut(&T) -> boolReturns an iterator over subslices separated by elements that match
pred, limited to returning at most n items. The matched element is
not contained in the subslices.
The last element returned, if any, will contain the remainder of the slice.
fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<T, F> where F: FnMut(&T) -> boolReturns an iterator over subslices separated by elements that match
pred limited to returning at most n items. This starts at the end of
the slice and works backwards. The matched element is not contained in
the subslices.
The last element returned, if any, will contain the remainder of the slice.
Print the slice split once, starting from the end, by numbers divisible
by 3 (i.e. [50], [10, 40, 30, 20]):
let v = [10, 40, 30, 20, 60, 50]; for group in v.rsplitn(2, |num| *num % 3 == 0) { println!("{:?}", group); }
fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<T, F> where F: FnMut(&T) -> boolReturns an iterator over subslices separated by elements that match
pred limited to returning at most n items. This starts at the end of
the slice and works backwards. The matched element is not contained in
the subslices.
The last element returned, if any, will contain the remainder of the slice.
fn split_off(&mut self, at: usize) -> Vec<T>Splits the collection into two at the given index.
Returns a newly allocated Self. self contains elements [0, at),
and the returned Self contains elements [at, len).
Note that the capacity of self does not change.
Panics if at > len.
let mut vec = vec![1,2,3]; let vec2 = vec.split_off(1); assert_eq!(vec, [1]); assert_eq!(vec2, [2, 3]);
fn cmp(&self, other: &Vec<T>) -> OrderingThis method returns an Ordering between self and other. Read more
fn eq(&self, other: &Vec<B>) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn lt(&self, other: &Rhs) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, other: &Rhs) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, other: &Rhs) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, other: &Rhs) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
fn hash<H>(&self, state: &mut H) where H: HasherFeeds this value into the state given, updating the hasher as necessary.
impl<T> From<BinaryHeap<T>> for Vec<T>fn from(heap: BinaryHeap<T>) -> Vec<T>Performs the conversion.
impl<T> Index<usize> for Vec<T>type Output = TThe returned type after indexing
fn index(&self, index: usize) -> &TThe method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<usize> for Vec<T>impl<T> Index<Range<usize>> for Vec<T>type Output = [T]The returned type after indexing
fn index(&self, index: Range<usize>) -> &[T]The method for the indexing (Foo[Bar]) operation
impl<T> Index<RangeTo<usize>> for Vec<T>type Output = [T]The returned type after indexing
fn index(&self, index: RangeTo<usize>) -> &[T]The method for the indexing (Foo[Bar]) operation
impl<T> Index<RangeFrom<usize>> for Vec<T>type Output = [T]The returned type after indexing
fn index(&self, index: RangeFrom<usize>) -> &[T]The method for the indexing (Foo[Bar]) operation
impl<T> Index<RangeFull> for Vec<T>type Output = [T]The returned type after indexing
fn index(&self, _index: RangeFull) -> &[T]The method for the indexing (Foo[Bar]) operation
impl<T> Index<RangeInclusive<usize>> for Vec<T>type Output = [T]The returned type after indexing
fn index(&self, index: RangeInclusive<usize>) -> &[T]The method for the indexing (Foo[Bar]) operation
impl<T> Index<RangeToInclusive<usize>> for Vec<T>type Output = [T]The returned type after indexing
fn index(&self, index: RangeToInclusive<usize>) -> &[T]The method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<Range<usize>> for Vec<T>fn index_mut(&mut self, index: Range<usize>) -> &mut [T]The method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<RangeTo<usize>> for Vec<T>fn index_mut(&mut self, index: RangeTo<usize>) -> &mut [T]The method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<RangeFrom<usize>> for Vec<T>fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut [T]The method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<RangeFull> for Vec<T>fn index_mut(&mut self, _index: RangeFull) -> &mut [T]The method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<RangeInclusive<usize>> for Vec<T>fn index_mut(&mut self, index: RangeInclusive<usize>) -> &mut [T]The method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<RangeToInclusive<usize>> for Vec<T>fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut [T]The method for the indexing (Foo[Bar]) operation
impl<T> FromIterator<T> for Vec<T>fn from_iter<I>(iter: I) -> Vec<T> where I: IntoIterator<Item=T>Creates a value from an iterator. Read more
impl<T> IntoIterator for Vec<T>type Item = TThe type of the elements being iterated over.
type IntoIter = IntoIter<T>Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<T>Creates a consuming iterator, that is, one that moves each value out of the vector (from start to end). The vector cannot be used after calling this.
let v = vec!["a".to_string(), "b".to_string()]; for s in v.into_iter() { // s has type String, not &String println!("{}", s); }
impl<'a, T> IntoIterator for &'a Vec<T>type Item = &'a TThe type of the elements being iterated over.
type IntoIter = Iter<'a, T>Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, T>Creates an iterator from a value. Read more
impl<'a, T> IntoIterator for &'a mut Vec<T>impl<T> Extend<T> for Vec<T>fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=T>Extends a collection with the contents of an iterator. Read more
impl<'a, T> Extend<&'a T> for Vec<T> where T: Copy + 'afn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=&'a T>Extends a collection with the contents of an iterator. Read more
impl<'a, 'b, A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &Vec<B>) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &Vec<B>) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b mut [B]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b mut [B]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 0]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 0]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 0]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 0]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 0]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 0]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 1]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 1]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 1]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 1]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 1]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 1]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 2]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 2]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 2]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 2]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 2]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 2]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 3]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 3]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 3]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 3]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 3]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 3]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 4]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 4]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 4]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 4]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 4]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 4]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 5]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 5]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 5]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 5]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 5]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 5]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 6]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 6]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 6]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 6]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 6]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 6]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 7]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 7]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 7]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 7]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 7]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 7]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 8]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 8]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 8]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 8]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 8]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 8]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 9]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 9]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 9]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 9]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 9]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 9]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 10]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 10]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 10]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 10]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 10]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 10]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 11]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 11]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 11]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 11]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 11]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 11]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 12]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 12]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 12]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 12]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 12]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 12]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 13]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 13]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 13]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 13]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 13]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 13]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 14]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 14]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 14]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 14]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 14]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 14]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 15]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 15]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 15]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 15]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 15]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 15]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 16]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 16]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 16]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 16]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 16]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 16]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 17]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 17]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 17]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 17]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 17]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 17]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 18]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 18]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 18]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 18]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 18]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 18]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 19]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 19]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 19]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 19]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 19]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 19]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 20]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 20]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 20]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 20]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 20]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 20]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 21]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 21]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 21]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 21]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 21]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 21]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 22]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 22]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 22]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 22]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 22]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 22]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 23]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 23]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 23]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 23]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 23]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 23]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 24]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 24]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 24]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 24]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 24]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 24]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 25]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 25]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 25]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 25]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 25]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 25]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 26]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 26]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 26]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 26]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 26]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 26]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 27]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 27]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 27]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 27]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 27]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 27]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 28]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 28]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 28]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 28]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 28]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 28]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 29]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 29]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 29]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 29]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 29]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 29]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 30]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 30]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 30]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 30]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 30]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 30]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 31]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 31]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 31]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 31]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 31]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 31]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<[B; 32]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &[B; 32]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &[B; 32]) -> boolThis method tests for !=.
impl<'a, 'b, A, B> PartialEq<&'b [B; 32]> for Vec<A> where A: PartialEq<B>fn eq(&self, other: &&'b [B; 32]) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &&'b [B; 32]) -> boolThis method tests for !=.
impl<T> PartialOrd<Vec<T>> for Vec<T> where T: PartialOrd<T>fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering>This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, other: &Rhs) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, other: &Rhs) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, other: &Rhs) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, other: &Rhs) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl<T> Eq for Vec<T> where T: Eqimpl<T> Ord for Vec<T> where T: Ordfn cmp(&self, other: &Vec<T>) -> OrderingThis method returns an Ordering between self and other. Read more
impl<T> Drop for Vec<T>impl<T> Default for Vec<T>impl<T> Debug for Vec<T> where T: Debugimpl<T> AsRef<Vec<T>> for Vec<T>impl<T> AsMut<Vec<T>> for Vec<T>impl<T> AsRef<[T]> for Vec<T>impl<T> AsMut<[T]> for Vec<T>impl<'a, T> From<&'a [T]> for Vec<T> where T: Cloneimpl<'a> From<&'a str> for Vec<u8>impl<T> From<VecDeque<T>> for Vec<T>impl From<CString> for Vec<u8>impl Write for Vec<u8>[src]fn write(&mut self, buf: &[u8]) -> Result<usize>Write a buffer into this object, returning how many bytes were written. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<()>Attempts to write an entire buffer into this write. Read more
fn flush(&mut self) -> Result<()>Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<()>Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self where Self: SizedCreates a "by reference" adaptor for this instance of Write. Read more
impl<T> PartialOrd<Vec<T>> for Vec<T> where T: PartialOrd<T>fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering>This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, other: &Rhs) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, other: &Rhs) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, other: &Rhs) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, other: &Rhs) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl Write for Vec<u8>[src]fn write(&mut self, buf: &[u8]) -> Result<usize>Write a buffer into this object, returning how many bytes were written. Read more
fn write_all(&mut self, buf: &[u8]) -> Result<()>Attempts to write an entire buffer into this write. Read more
fn flush(&mut self) -> Result<()>Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<()>Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self where Self: SizedCreates a "by reference" adaptor for this instance of Write. Read more
macro_rules! vec { ( $ elem : expr ; $ n : expr ) => { ... }; ( $ ( $ x : expr ) , * ) => { ... }; ( $ ( $ x : expr , ) * ) => { ... }; }
Creates a Vec containing the arguments.
vec! allows Vecs to be defined with the same syntax as array expressions.
There are two forms of this macro:
Vec containing a given list of elements:let v = vec![1, 2, 3]; assert_eq!(v[0], 1); assert_eq!(v[1], 2); assert_eq!(v[2], 3);
Vec from a given element and size:let v = vec![1; 3]; assert_eq!(v, [1, 1, 1]);
Note that unlike array expressions this syntax supports all elements
which implement Clone and the number of elements doesn't have to be
a constant.
This will use clone() to duplicate an expression, so one should be careful
using this with types having a nonstandard Clone implementation. For
example, vec![Rc::new(1); 5] will create a vector of five references
to the same boxed integer value, not five references pointing to independently
boxed integers.
pub trait IntoIterator where Self::IntoIter::Item == Self::Item { type Item; type IntoIter: Iterator; fn into_iter(self) -> Self::IntoIter; }
Conversion into an Iterator.
By implementing IntoIterator for a type, you define how it will be
converted to an iterator. This is common for types which describe a
collection of some kind.
One benefit of implementing IntoIterator is that your type will work
with Rust's for loop syntax.
See also: FromIterator.
Basic usage:
fn main() { let v = vec![1, 2, 3]; let mut iter = v.into_iter(); let n = iter.next(); assert_eq!(Some(1), n); let n = iter.next(); assert_eq!(Some(2), n); let n = iter.next(); assert_eq!(Some(3), n); let n = iter.next(); assert_eq!(None, n); }let v = vec![1, 2, 3]; let mut iter = v.into_iter(); let n = iter.next(); assert_eq!(Some(1), n); let n = iter.next(); assert_eq!(Some(2), n); let n = iter.next(); assert_eq!(Some(3), n); let n = iter.next(); assert_eq!(None, n);
Implementing IntoIterator for your type:
// A sample collection, that's just a wrapper over Vec<T> #[derive(Debug)] struct MyCollection(Vec<i32>); // Let's give it some methods so we can create one and add things // to it. impl MyCollection { fn new() -> MyCollection { MyCollection(Vec::new()) } fn add(&mut self, elem: i32) { self.0.push(elem); } } // and we'll implement IntoIterator impl IntoIterator for MyCollection { type Item = i32; type IntoIter = ::std::vec::IntoIter<i32>; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } // Now we can make a new collection... let mut c = MyCollection::new(); // ... add some stuff to it ... c.add(0); c.add(1); c.add(2); // ... and then turn it into an Iterator: for (i, n) in c.into_iter().enumerate() { assert_eq!(i as i32, n); }
type ItemThe type of the elements being iterated over.
type IntoIter: IteratorWhich kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIterCreates an iterator from a value.
See the module-level documentation for more.
Basic usage:
fn main() { let v = vec![1, 2, 3]; let mut iter = v.into_iter(); let n = iter.next(); assert_eq!(Some(1), n); let n = iter.next(); assert_eq!(Some(2), n); let n = iter.next(); assert_eq!(Some(3), n); let n = iter.next(); assert_eq!(None, n); }let v = vec![1, 2, 3]; let mut iter = v.into_iter(); let n = iter.next(); assert_eq!(Some(1), n); let n = iter.next(); assert_eq!(Some(2), n); let n = iter.next(); assert_eq!(Some(3), n); let n = iter.next(); assert_eq!(None, n);
impl<'a, T> IntoIterator for &'a [T; 0]impl<'a, T> IntoIterator for &'a mut [T; 0]impl<'a, T> IntoIterator for &'a [T; 1]impl<'a, T> IntoIterator for &'a mut [T; 1]impl<'a, T> IntoIterator for &'a [T; 2]impl<'a, T> IntoIterator for &'a mut [T; 2]impl<'a, T> IntoIterator for &'a [T; 3]impl<'a, T> IntoIterator for &'a mut [T; 3]impl<'a, T> IntoIterator for &'a [T; 4]impl<'a, T> IntoIterator for &'a mut [T; 4]impl<'a, T> IntoIterator for &'a [T; 5]impl<'a, T> IntoIterator for &'a mut [T; 5]impl<'a, T> IntoIterator for &'a [T; 6]impl<'a, T> IntoIterator for &'a mut [T; 6]impl<'a, T> IntoIterator for &'a [T; 7]impl<'a, T> IntoIterator for &'a mut [T; 7]impl<'a, T> IntoIterator for &'a [T; 8]impl<'a, T> IntoIterator for &'a mut [T; 8]impl<'a, T> IntoIterator for &'a [T; 9]impl<'a, T> IntoIterator for &'a mut [T; 9]impl<'a, T> IntoIterator for &'a [T; 10]impl<'a, T> IntoIterator for &'a mut [T; 10]impl<'a, T> IntoIterator for &'a [T; 11]impl<'a, T> IntoIterator for &'a mut [T; 11]impl<'a, T> IntoIterator for &'a [T; 12]impl<'a, T> IntoIterator for &'a mut [T; 12]impl<'a, T> IntoIterator for &'a [T; 13]impl<'a, T> IntoIterator for &'a mut [T; 13]impl<'a, T> IntoIterator for &'a [T; 14]impl<'a, T> IntoIterator for &'a mut [T; 14]impl<'a, T> IntoIterator for &'a [T; 15]impl<'a, T> IntoIterator for &'a mut [T; 15]impl<'a, T> IntoIterator for &'a [T; 16]impl<'a, T> IntoIterator for &'a mut [T; 16]impl<'a, T> IntoIterator for &'a [T; 17]impl<'a, T> IntoIterator for &'a mut [T; 17]impl<'a, T> IntoIterator for &'a [T; 18]impl<'a, T> IntoIterator for &'a mut [T; 18]impl<'a, T> IntoIterator for &'a [T; 19]impl<'a, T> IntoIterator for &'a mut [T; 19]impl<'a, T> IntoIterator for &'a [T; 20]impl<'a, T> IntoIterator for &'a mut [T; 20]impl<'a, T> IntoIterator for &'a [T; 21]impl<'a, T> IntoIterator for &'a mut [T; 21]impl<'a, T> IntoIterator for &'a [T; 22]impl<'a, T> IntoIterator for &'a mut [T; 22]impl<'a, T> IntoIterator for &'a [T; 23]impl<'a, T> IntoIterator for &'a mut [T; 23]impl<'a, T> IntoIterator for &'a [T; 24]impl<'a, T> IntoIterator for &'a mut [T; 24]impl<'a, T> IntoIterator for &'a [T; 25]impl<'a, T> IntoIterator for &'a mut [T; 25]impl<'a, T> IntoIterator for &'a [T; 26]impl<'a, T> IntoIterator for &'a mut [T; 26]impl<'a, T> IntoIterator for &'a [T; 27]impl<'a, T> IntoIterator for &'a mut [T; 27]impl<'a, T> IntoIterator for &'a [T; 28]impl<'a, T> IntoIterator for &'a mut [T; 28]impl<'a, T> IntoIterator for &'a [T; 29]impl<'a, T> IntoIterator for &'a mut [T; 29]impl<'a, T> IntoIterator for &'a [T; 30]impl<'a, T> IntoIterator for &'a mut [T; 30]impl<'a, T> IntoIterator for &'a [T; 31]impl<'a, T> IntoIterator for &'a mut [T; 31]impl<'a, T> IntoIterator for &'a [T; 32]impl<'a, T> IntoIterator for &'a mut [T; 32]impl<I> IntoIterator for I where I: Iteratorimpl<T> IntoIterator for Option<T>impl<'a, T> IntoIterator for &'a Option<T>impl<'a, T> IntoIterator for &'a mut Option<T>impl<T, E> IntoIterator for Result<T, E>impl<'a, T, E> IntoIterator for &'a Result<T, E>impl<'a, T, E> IntoIterator for &'a mut Result<T, E>impl<'a, T> IntoIterator for &'a [T]impl<'a, T> IntoIterator for &'a mut [T]impl<T> IntoIterator for BinaryHeap<T> where T: Ordimpl<'a, T> IntoIterator for &'a BinaryHeap<T> where T: Ordimpl<'a, K, V> IntoIterator for &'a BTreeMap<K, V> where K: 'a, V: 'aimpl<'a, K, V> IntoIterator for &'a mut BTreeMap<K, V> where K: 'a, V: 'aimpl<K, V> IntoIterator for BTreeMap<K, V>impl<T> IntoIterator for BTreeSet<T>impl<'a, T> IntoIterator for &'a BTreeSet<T>impl<'a, E> IntoIterator for &'a EnumSet<E> where E: CLikeimpl<T> IntoIterator for LinkedList<T>impl<'a, T> IntoIterator for &'a LinkedList<T>impl<'a, T> IntoIterator for &'a mut LinkedList<T>impl<T> IntoIterator for Vec<T>impl<'a, T> IntoIterator for &'a Vec<T>impl<'a, T> IntoIterator for &'a mut Vec<T>impl<T> IntoIterator for VecDeque<T>impl<'a, T> IntoIterator for &'a VecDeque<T>impl<'a, T> IntoIterator for &'a mut VecDeque<T>impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> where K: Eq + Hash, S: BuildHasherimpl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> where K: Eq + Hash, S: BuildHasherimpl<K, V, S> IntoIterator for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasherimpl<'a, T, S> IntoIterator for &'a HashSet<T, S> where T: Eq + Hash, S: BuildHasherimpl<T, S> IntoIterator for HashSet<T, S> where T: Eq + Hash, S: BuildHasherimpl<'a> IntoIterator for &'a UnixListenerimpl<'a> IntoIterator for &'a PathBufimpl<'a> IntoIterator for &'a Pathimpl<'a, T> IntoIterator for &'a Receiver<T>impl<T> IntoIterator for Receiver<T>pub trait FromIterator<A> {
fn from_iter<T>(iter: T) -> Self where T: IntoIterator<Item=A>;
}Conversion from an Iterator.
By implementing FromIterator for a type, you define how it will be
created from an iterator. This is common for types which describe a
collection of some kind.
FromIterator's from_iter() is rarely called explicitly, and is instead
used through Iterator's collect() method. See collect()'s
documentation for more examples.
See also: IntoIterator.
Basic usage:
fn main() { use std::iter::FromIterator; let five_fives = std::iter::repeat(5).take(5); let v = Vec::from_iter(five_fives); assert_eq!(v, vec![5, 5, 5, 5, 5]); }use std::iter::FromIterator; let five_fives = std::iter::repeat(5).take(5); let v = Vec::from_iter(five_fives); assert_eq!(v, vec![5, 5, 5, 5, 5]);
Using collect() to implicitly use FromIterator:
let five_fives = std::iter::repeat(5).take(5); let v: Vec<i32> = five_fives.collect(); assert_eq!(v, vec![5, 5, 5, 5, 5]);
Implementing FromIterator for your type:
use std::iter::FromIterator; // A sample collection, that's just a wrapper over Vec<T> #[derive(Debug)] struct MyCollection(Vec<i32>); // Let's give it some methods so we can create one and add things // to it. impl MyCollection { fn new() -> MyCollection { MyCollection(Vec::new()) } fn add(&mut self, elem: i32) { self.0.push(elem); } } // and we'll implement FromIterator impl FromIterator<i32> for MyCollection { fn from_iter<I: IntoIterator<Item=i32>>(iter: I) -> Self { let mut c = MyCollection::new(); for i in iter { c.add(i); } c } } // Now we can make a new iterator... let iter = (0..5).into_iter(); // ... and make a MyCollection out of it let c = MyCollection::from_iter(iter); assert_eq!(c.0, vec![0, 1, 2, 3, 4]); // collect works too! let iter = (0..5).into_iter(); let c: MyCollection = iter.collect(); assert_eq!(c.0, vec![0, 1, 2, 3, 4]);
fn from_iter<T>(iter: T) -> Self where T: IntoIterator<Item=A>Creates a value from an iterator.
See the module-level documentation for more.
Basic usage:
fn main() { use std::iter::FromIterator; let five_fives = std::iter::repeat(5).take(5); let v = Vec::from_iter(five_fives); assert_eq!(v, vec![5, 5, 5, 5, 5]); }use std::iter::FromIterator; let five_fives = std::iter::repeat(5).take(5); let v = Vec::from_iter(five_fives); assert_eq!(v, vec![5, 5, 5, 5, 5]);
impl<A, V> FromIterator<Option<A>> for Option<V> where V: FromIterator<A>impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E> where V: FromIterator<A>impl<T> FromIterator<T> for BinaryHeap<T> where T: Ordimpl<K, V> FromIterator<(K, V)> for BTreeMap<K, V> where K: Ordimpl<T> FromIterator<T> for BTreeSet<T> where T: Ordimpl<E> FromIterator<E> for EnumSet<E> where E: CLikeimpl<A> FromIterator<A> for LinkedList<A>impl FromIterator<char> for Stringimpl<'a> FromIterator<&'a str> for Stringimpl FromIterator<String> for Stringimpl<T> FromIterator<T> for Vec<T>impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Cloneimpl<A> FromIterator<A> for VecDeque<A>impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher + Defaultimpl<T, S> FromIterator<T> for HashSet<T, S> where T: Eq + Hash, S: BuildHasher + Defaultimpl<P: AsRef<Path>> FromIterator<P> for PathBufpub trait PartialEq<Rhs = Self> where Rhs: ?Sized { fn eq(&self, other: &Rhs) -> bool; fn ne(&self, other: &Rhs) -> bool { ... } }
Trait for equality comparisons which are partial equivalence relations.
This trait allows for partial equality, for types that do not have a full
equivalence relation. For example, in floating point numbers NaN != NaN,
so floating point types implement PartialEq but not Eq.
Formally, the equality must be (for all a, b and c):
a == b implies b == a; anda == b and b == c implies a == c.Note that these requirements mean that the trait itself must be implemented
symmetrically and transitively: if T: PartialEq<U> and U: PartialEq<V>
then U: PartialEq<T> and T: PartialEq<V>.
This trait can be used with #[derive]. When derived on structs, two
instances are equal if all fields are equal, and not equal if any fields
are not equal. When derived on enums, each variant is equal to itself
and not equal to the other variants.
PartialEq?PartialEq only requires the eq method to be implemented; ne is defined
in terms of it by default. Any manual implementation of ne must respect
the rule that eq is a strict inverse of ne; that is, !(a == b) if and
only if a != b.
An example implementation for a domain in which two books are considered the same book if their ISBN matches, even if the formats differ:
fn main() { enum BookFormat { Paperback, Hardback, Ebook } struct Book { isbn: i32, format: BookFormat, } impl PartialEq for Book { fn eq(&self, other: &Book) -> bool { self.isbn == other.isbn } } let b1 = Book { isbn: 3, format: BookFormat::Paperback }; let b2 = Book { isbn: 3, format: BookFormat::Ebook }; let b3 = Book { isbn: 10, format: BookFormat::Paperback }; assert!(b1 == b2); assert!(b1 != b3); }enum BookFormat { Paperback, Hardback, Ebook } struct Book { isbn: i32, format: BookFormat, } impl PartialEq for Book { fn eq(&self, other: &Book) -> bool { self.isbn == other.isbn } } let b1 = Book { isbn: 3, format: BookFormat::Paperback }; let b2 = Book { isbn: 3, format: BookFormat::Ebook }; let b3 = Book { isbn: 10, format: BookFormat::Paperback }; assert!(b1 == b2); assert!(b1 != b3);
let x: u32 = 0; let y: u32 = 1; assert_eq!(x == y, false); assert_eq!(x.eq(&y), false);
fn eq(&self, other: &Rhs) -> boolThis method tests for self and other values to be equal, and is used
by ==.
impl<T> PartialEq<Wrapping<T>> for Wrapping<T> where T: PartialEq<T>impl PartialEq<ParseFloatError> for ParseFloatErrorimpl PartialEq<FpCategory> for FpCategoryimpl PartialEq<ParseIntError> for ParseIntErrorimpl<T> PartialEq<NonZero<T>> for NonZero<T> where T: Zeroable + PartialEq<T>impl<T> PartialEq<*const T> for *const T where T: ?Sizedimpl<T> PartialEq<*mut T> for *mut T where T: ?Sizedimpl<Ret> PartialEq<fn() -> Ret> for fn() -> Retimpl<Ret> PartialEq<extern fn() -> Ret> for extern fn() -> Retimpl<Ret> PartialEq<unsafe fn() -> Ret> for unsafe fn() -> Retimpl<Ret> PartialEq<unsafe extern fn() -> Ret> for unsafe extern fn() -> Retimpl<Ret, A> PartialEq<fn(A) -> Ret> for fn(A) -> Retimpl<Ret, A> PartialEq<extern fn(A) -> Ret> for extern fn(A) -> Retimpl<Ret, A> PartialEq<unsafe fn(A) -> Ret> for unsafe fn(A) -> Retimpl<Ret, A> PartialEq<unsafe extern fn(A) -> Ret> for unsafe extern fn(A) -> Retimpl<Ret, A, B> PartialEq<fn(A, B) -> Ret> for fn(A, B) -> Retimpl<Ret, A, B> PartialEq<extern fn(A, B) -> Ret> for extern fn(A, B) -> Retimpl<Ret, A, B> PartialEq<unsafe fn(A, B) -> Ret> for unsafe fn(A, B) -> Retimpl<Ret, A, B> PartialEq<unsafe extern fn(A, B) -> Ret> for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> PartialEq<fn(A, B, C) -> Ret> for fn(A, B, C) -> Retimpl<Ret, A, B, C> PartialEq<extern fn(A, B, C) -> Ret> for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> PartialEq<unsafe fn(A, B, C) -> Ret> for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> PartialEq<unsafe extern fn(A, B, C) -> Ret> for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> PartialEq<fn(A, B, C, D) -> Ret> for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> PartialEq<extern fn(A, B, C, D) -> Ret> for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> PartialEq<unsafe fn(A, B, C, D) -> Ret> for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> PartialEq<unsafe extern fn(A, B, C, D) -> Ret> for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> PartialEq<fn(A, B, C, D, E) -> Ret> for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> PartialEq<extern fn(A, B, C, D, E) -> Ret> for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> PartialEq<unsafe fn(A, B, C, D, E) -> Ret> for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> PartialEq<unsafe extern fn(A, B, C, D, E) -> Ret> for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> PartialEq<fn(A, B, C, D, E, F) -> Ret> for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> PartialEq<extern fn(A, B, C, D, E, F) -> Ret> for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> PartialEq<unsafe fn(A, B, C, D, E, F) -> Ret> for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> PartialEq<unsafe extern fn(A, B, C, D, E, F) -> Ret> for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialEq<fn(A, B, C, D, E, F, G) -> Ret> for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialEq<extern fn(A, B, C, D, E, F, G) -> Ret> for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialEq<unsafe fn(A, B, C, D, E, F, G) -> Ret> for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialEq<unsafe extern fn(A, B, C, D, E, F, G) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialEq<fn(A, B, C, D, E, F, G, H) -> Ret> for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialEq<extern fn(A, B, C, D, E, F, G, H) -> Ret> for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialEq<unsafe fn(A, B, C, D, E, F, G, H) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialEq<unsafe extern fn(A, B, C, D, E, F, G, H) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialEq<fn(A, B, C, D, E, F, G, H, I) -> Ret> for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialEq<extern fn(A, B, C, D, E, F, G, H, I) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialEq<unsafe fn(A, B, C, D, E, F, G, H, I) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialEq<unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialEq<fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialEq<extern fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialEq<unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialEq<unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialEq<fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialEq<extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialEq<unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialEq<unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<T> PartialEq<PhantomData<T>> for PhantomData<T> where T: ?Sizedimpl PartialEq<RangeFull> for RangeFullimpl<Idx> PartialEq<Range<Idx>> for Range<Idx> where Idx: PartialEq<Idx>impl<Idx> PartialEq<RangeFrom<Idx>> for RangeFrom<Idx> where Idx: PartialEq<Idx>impl<Idx> PartialEq<RangeTo<Idx>> for RangeTo<Idx> where Idx: PartialEq<Idx>impl<Idx> PartialEq<RangeInclusive<Idx>> for RangeInclusive<Idx> where Idx: PartialEq<Idx>impl<Idx> PartialEq<RangeToInclusive<Idx>> for RangeToInclusive<Idx> where Idx: PartialEq<Idx>impl PartialEq<Ordering> for Orderingimpl PartialEq<()> for ()impl PartialEq<bool> for boolimpl PartialEq<char> for charimpl PartialEq<usize> for usizeimpl PartialEq<u8> for u8impl PartialEq<u16> for u16impl PartialEq<u32> for u32impl PartialEq<u64> for u64impl PartialEq<isize> for isizeimpl PartialEq<i8> for i8impl PartialEq<i16> for i16impl PartialEq<i32> for i32impl PartialEq<i64> for i64impl PartialEq<f32> for f32impl PartialEq<f64> for f64impl<'a, 'b, A, B> PartialEq<&'b B> for &'a A where A: PartialEq<B> + ?Sized, B: ?Sizedimpl<'a, 'b, A, B> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> + ?Sized, B: ?Sizedimpl<'a, 'b, A, B> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> + ?Sized, B: ?Sizedimpl<'a, 'b, A, B> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> + ?Sized, B: ?Sizedimpl PartialEq<TypeId> for TypeIdimpl<'a, 'b, A, B> PartialEq<[B; 0]> for [A; 0] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 0] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 0]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 0] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 0]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 0] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 0]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 1]> for [A; 1] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 1] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 1]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 1] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 1]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 1] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 1]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 2]> for [A; 2] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 2] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 2]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 2] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 2]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 2] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 2]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 3]> for [A; 3] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 3] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 3]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 3] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 3]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 3] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 3]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 4]> for [A; 4] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 4] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 4]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 4] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 4]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 4] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 4]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 5]> for [A; 5] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 5] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 5]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 5] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 5]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 5] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 5]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 6]> for [A; 6] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 6] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 6]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 6] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 6]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 6] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 6]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 7]> for [A; 7] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 7] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 7]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 7] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 7]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 7] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 7]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 8]> for [A; 8] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 8] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 8]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 8] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 8]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 8] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 8]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 9]> for [A; 9] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 9] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 9]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 9] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 9]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 9] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 9]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 10]> for [A; 10] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 10] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 10]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 10] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 10]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 10] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 10]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 11]> for [A; 11] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 11] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 11]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 11] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 11]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 11] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 11]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 12]> for [A; 12] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 12] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 12]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 12] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 12]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 12] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 12]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 13]> for [A; 13] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 13] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 13]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 13] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 13]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 13] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 13]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 14]> for [A; 14] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 14] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 14]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 14] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 14]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 14] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 14]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 15]> for [A; 15] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 15] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 15]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 15] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 15]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 15] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 15]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 16]> for [A; 16] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 16] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 16]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 16] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 16]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 16] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 16]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 17]> for [A; 17] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 17] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 17]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 17] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 17]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 17] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 17]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 18]> for [A; 18] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 18] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 18]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 18] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 18]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 18] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 18]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 19]> for [A; 19] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 19] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 19]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 19] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 19]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 19] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 19]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 20]> for [A; 20] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 20] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 20]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 20] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 20]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 20] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 20]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 21]> for [A; 21] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 21] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 21]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 21] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 21]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 21] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 21]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 22]> for [A; 22] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 22] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 22]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 22] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 22]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 22] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 22]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 23]> for [A; 23] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 23] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 23]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 23] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 23]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 23] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 23]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 24]> for [A; 24] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 24] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 24]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 24] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 24]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 24] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 24]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 25]> for [A; 25] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 25] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 25]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 25] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 25]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 25] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 25]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 26]> for [A; 26] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 26] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 26]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 26] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 26]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 26] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 26]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 27]> for [A; 27] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 27] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 27]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 27] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 27]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 27] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 27]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 28]> for [A; 28] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 28] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 28]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 28] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 28]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 28] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 28]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 29]> for [A; 29] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 29] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 29]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 29] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 29]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 29] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 29]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 30]> for [A; 30] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 30] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 30]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 30] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 30]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 30] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 30]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 31]> for [A; 31] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 31] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 31]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 31] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 31]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 31] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 31]> for &'b mut [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<[B; 32]> for [A; 32] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B]> for [A; 32] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 32]> for [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b [B]> for [A; 32] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 32]> for &'b [B] where B: PartialEq<A>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for [A; 32] where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[A; 32]> for &'b mut [B] where B: PartialEq<A>impl<T> PartialEq<Cell<T>> for Cell<T> where T: Copy + PartialEq<T>impl PartialEq<BorrowState> for BorrowStateimpl<T> PartialEq<RefCell<T>> for RefCell<T> where T: PartialEq<T> + ?Sizedimpl<T> PartialEq<Option<T>> for Option<T> where T: PartialEq<T>impl<T, E> PartialEq<Result<T, E>> for Result<T, E> where E: PartialEq<E>, T: PartialEq<T>impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B>impl PartialEq<SearchStep> for SearchStepimpl PartialEq<ParseBoolError> for ParseBoolErrorimpl PartialEq<Utf8Error> for Utf8Errorimpl PartialEq<str> for strimpl PartialEq<Error> for Errorimpl<A> PartialEq<(A,)> for (A,) where A: PartialEq<A>impl<A, B> PartialEq<(A, B)> for (A, B) where A: PartialEq<A>, B: PartialEq<B>impl<A, B, C> PartialEq<(A, B, C)> for (A, B, C) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>impl<A, B, C, D> PartialEq<(A, B, C, D)> for (A, B, C, D) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>impl<A, B, C, D, E> PartialEq<(A, B, C, D, E)> for (A, B, C, D, E) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>impl<A, B, C, D, E, F> PartialEq<(A, B, C, D, E, F)> for (A, B, C, D, E, F) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>impl<A, B, C, D, E, F, G> PartialEq<(A, B, C, D, E, F, G)> for (A, B, C, D, E, F, G) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>, G: PartialEq<G>impl<A, B, C, D, E, F, G, H> PartialEq<(A, B, C, D, E, F, G, H)> for (A, B, C, D, E, F, G, H) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>, G: PartialEq<G>, H: PartialEq<H>impl<A, B, C, D, E, F, G, H, I> PartialEq<(A, B, C, D, E, F, G, H, I)> for (A, B, C, D, E, F, G, H, I) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>, G: PartialEq<G>, H: PartialEq<H>, I: PartialEq<I>impl<A, B, C, D, E, F, G, H, I, J> PartialEq<(A, B, C, D, E, F, G, H, I, J)> for (A, B, C, D, E, F, G, H, I, J) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>, G: PartialEq<G>, H: PartialEq<H>, I: PartialEq<I>, J: PartialEq<J>impl<A, B, C, D, E, F, G, H, I, J, K> PartialEq<(A, B, C, D, E, F, G, H, I, J, K)> for (A, B, C, D, E, F, G, H, I, J, K) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>, G: PartialEq<G>, H: PartialEq<H>, I: PartialEq<I>, J: PartialEq<J>, K: PartialEq<K>impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialEq<(A, B, C, D, E, F, G, H, I, J, K, L)> for (A, B, C, D, E, F, G, H, I, J, K, L) where A: PartialEq<A>, B: PartialEq<B>, C: PartialEq<C>, D: PartialEq<D>, E: PartialEq<E>, F: PartialEq<F>, G: PartialEq<G>, H: PartialEq<H>, I: PartialEq<I>, J: PartialEq<J>, K: PartialEq<K>, L: PartialEq<L>impl<T> PartialEq<Box<T>> for Box<T> where T: PartialEq<T> + ?Sizedimpl<T> PartialEq<Arc<T>> for Arc<T> where T: PartialEq<T> + ?Sizedimpl<T> PartialEq<Rc<T>> for Rc<T> where T: PartialEq<T> + ?Sizedimpl<K, V> PartialEq<BTreeMap<K, V>> for BTreeMap<K, V> where K: PartialEq<K>, V: PartialEq<V>impl<T> PartialEq<BTreeSet<T>> for BTreeSet<T> where T: PartialEq<T>impl<'a, 'b, B, C> PartialEq<Cow<'b, C>> for Cow<'a, B> where B: PartialEq<C> + ToOwned + ?Sized, C: ToOwned + ?Sizedimpl<E> PartialEq<EnumSet<E>> for EnumSet<E> where E: PartialEq<E>impl<A> PartialEq<LinkedList<A>> for LinkedList<A> where A: PartialEq<A>impl PartialEq<String> for Stringimpl<'a, 'b> PartialEq<str> for Stringimpl<'a, 'b> PartialEq<String> for strimpl<'a, 'b> PartialEq<&'a str> for Stringimpl<'a, 'b> PartialEq<String> for &'a strimpl<'a, 'b> PartialEq<str> for Cow<'a, str>impl<'a, 'b> PartialEq<Cow<'a, str>> for strimpl<'a, 'b> PartialEq<&'b str> for Cow<'a, str>impl<'a, 'b> PartialEq<Cow<'a, str>> for &'b strimpl<'a, 'b> PartialEq<String> for Cow<'a, str>impl<'a, 'b> PartialEq<Cow<'a, str>> for Stringimpl PartialEq<ParseError> for ParseErrorimpl<'a, 'b, A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B]> for Cow<'a, [A]> where A: Clone + PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b mut [B]> for Cow<'a, [A]> where A: Clone + PartialEq<B>impl<'a, 'b, A, B> PartialEq<Vec<B>> for Cow<'a, [A]> where A: Clone + PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 0]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 0]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 1]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 1]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 2]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 2]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 3]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 3]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 4]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 4]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 5]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 5]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 6]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 6]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 7]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 7]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 8]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 8]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 9]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 9]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 10]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 10]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 11]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 11]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 12]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 12]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 13]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 13]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 14]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 14]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 15]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 15]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 16]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 16]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 17]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 17]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 18]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 18]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 19]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 19]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 20]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 20]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 21]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 21]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 22]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 22]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 23]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 23]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 24]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 24]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 25]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 25]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 26]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 26]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 27]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 27]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 28]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 28]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 29]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 29]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 30]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 30]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 31]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 31]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<[B; 32]> for Vec<A> where A: PartialEq<B>impl<'a, 'b, A, B> PartialEq<&'b [B; 32]> for Vec<A> where A: PartialEq<B>impl<A> PartialEq<VecDeque<A>> for VecDeque<A> where A: PartialEq<A>impl<T> PartialEq<Bound<T>> for Bound<T> where T: PartialEq<T>impl PartialEq<DecodeUtf16Error> for DecodeUtf16Errorimpl PartialEq for LocalKeyStateimpl<K, V, S> PartialEq for HashMap<K, V, S> where K: Eq + Hash, V: PartialEq, S: BuildHasherimpl<T, S> PartialEq for HashSet<T, S> where T: Eq + Hash, S: BuildHasherimpl PartialEq for VarErrorimpl PartialEq for CStringimpl PartialEq for NulErrorimpl PartialEq for FromBytesWithNulErrorimpl PartialEq for IntoStringErrorimpl PartialEq for CStrimpl PartialEq for OsStringimpl PartialEq<str> for OsStringimpl PartialEq<OsString> for strimpl PartialEq for OsStrimpl PartialEq<str> for OsStrimpl PartialEq<OsStr> for strimpl<'a, 'b> PartialEq<OsStr> for OsStringimpl<'a, 'b> PartialEq<OsString> for OsStrimpl<'a, 'b> PartialEq<&'a OsStr> for OsStringimpl<'a, 'b> PartialEq<OsString> for &'a OsStrimpl<'a, 'b> PartialEq<OsStr> for Cow<'a, OsStr>impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for OsStrimpl<'a, 'b> PartialEq<&'b OsStr> for Cow<'a, OsStr>impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for &'b OsStrimpl<'a, 'b> PartialEq<OsString> for Cow<'a, OsStr>impl<'a, 'b> PartialEq<Cow<'a, OsStr>> for OsStringimpl PartialEq for Permissionsimpl PartialEq for FileTypeimpl PartialEq for ErrorKindimpl PartialEq for SeekFromimpl PartialEq for IpAddrimpl PartialEq for Ipv6MulticastScopeimpl PartialEq for Ipv4Addrimpl PartialEq for Ipv6Addrimpl PartialEq for SocketAddrimpl PartialEq for SocketAddrV4impl PartialEq for SocketAddrV6impl PartialEq for AddrParseErrorimpl PartialEq for Shutdownimpl<'a> PartialEq for Prefix<'a>impl<'a> PartialEq for PrefixComponent<'a>impl<'a> PartialEq for Component<'a>impl<'a> PartialEq for Components<'a>impl PartialEq for PathBufimpl PartialEq for StripPrefixErrorimpl PartialEq for Pathimpl<'a, 'b> PartialEq<Path> for PathBufimpl<'a, 'b> PartialEq<PathBuf> for Pathimpl<'a, 'b> PartialEq<&'a Path> for PathBufimpl<'a, 'b> PartialEq<PathBuf> for &'a Pathimpl<'a, 'b> PartialEq<Path> for Cow<'a, Path>impl<'a, 'b> PartialEq<Cow<'a, Path>> for Pathimpl<'a, 'b> PartialEq<&'b Path> for Cow<'a, Path>impl<'a, 'b> PartialEq<Cow<'a, Path>> for &'b Pathimpl<'a, 'b> PartialEq<PathBuf> for Cow<'a, Path>impl<'a, 'b> PartialEq<Cow<'a, Path>> for PathBufimpl<'a, 'b> PartialEq<OsStr> for PathBufimpl<'a, 'b> PartialEq<PathBuf> for OsStrimpl<'a, 'b> PartialEq<&'a OsStr> for PathBufimpl<'a, 'b> PartialEq<PathBuf> for &'a OsStrimpl<'a, 'b> PartialEq<Cow<'a, OsStr>> for PathBufimpl<'a, 'b> PartialEq<PathBuf> for Cow<'a, OsStr>impl<'a, 'b> PartialEq<OsString> for PathBufimpl<'a, 'b> PartialEq<PathBuf> for OsStringimpl<'a, 'b> PartialEq<OsStr> for Pathimpl<'a, 'b> PartialEq<Path> for OsStrimpl<'a, 'b> PartialEq<&'a OsStr> for Pathimpl<'a, 'b> PartialEq<Path> for &'a OsStrimpl<'a, 'b> PartialEq<Cow<'a, OsStr>> for Pathimpl<'a, 'b> PartialEq<Path> for Cow<'a, OsStr>impl<'a, 'b> PartialEq<OsString> for Pathimpl<'a, 'b> PartialEq<Path> for OsStringimpl<'a, 'b> PartialEq<OsStr> for &'a Pathimpl<'a, 'b> PartialEq<&'a Path> for OsStrimpl<'a, 'b> PartialEq<Cow<'b, OsStr>> for &'a Pathimpl<'a, 'b> PartialEq<&'a Path> for Cow<'b, OsStr>impl<'a, 'b> PartialEq<OsString> for &'a Pathimpl<'a, 'b> PartialEq<&'a Path> for OsStringimpl<'a, 'b> PartialEq<OsStr> for Cow<'a, Path>impl<'a, 'b> PartialEq<Cow<'a, Path>> for OsStrimpl<'a, 'b> PartialEq<&'b OsStr> for Cow<'a, Path>impl<'a, 'b> PartialEq<Cow<'a, Path>> for &'b OsStrimpl<'a, 'b> PartialEq<OsString> for Cow<'a, Path>impl<'a, 'b> PartialEq<Cow<'a, Path>> for OsStringimpl PartialEq for Outputimpl PartialEq for ExitStatusimpl<T: PartialEq> PartialEq for SendError<T>impl PartialEq for RecvErrorimpl PartialEq for TryRecvErrorimpl PartialEq for RecvTimeoutErrorimpl<T: PartialEq> PartialEq for TrySendError<T>impl PartialEq for WaitTimeoutResultimpl PartialEq for Durationimpl PartialEq for Instantimpl PartialEq for SystemTimepub trait PartialOrd<Rhs = Self>: PartialEq<Rhs> where Rhs: ?Sized { fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>; fn lt(&self, other: &Rhs) -> bool { ... } fn le(&self, other: &Rhs) -> bool { ... } fn gt(&self, other: &Rhs) -> bool { ... } fn ge(&self, other: &Rhs) -> bool { ... } }
Trait for values that can be compared for a sort-order.
The comparison must satisfy, for all a, b and c:
a < b then !(a > b), as well as a > b implying !(a < b); anda < b and b < c implies a < c. The same must hold for both == and >.Note that these requirements mean that the trait itself must be implemented symmetrically and
transitively: if T: PartialOrd<U> and U: PartialOrd<V> then U: PartialOrd<T> and T: PartialOrd<V>.
This trait can be used with #[derive]. When derived, it will produce a lexicographic
ordering based on the top-to-bottom declaration order of the struct's members.
Ord?PartialOrd only requires implementation of the partial_cmp method, with the others generated
from default implementations.
However it remains possible to implement the others separately for types which do not have a
total order. For example, for floating point numbers, NaN < 0 == false and NaN >= 0 == false (cf. IEEE 754-2008 section 5.11).
PartialOrd requires your type to be PartialEq.
If your type is Ord, you can implement partial_cmp() by using cmp():
use std::cmp::Ordering; #[derive(Eq)] struct Person { id: u32, name: String, height: u32, } impl PartialOrd for Person { fn partial_cmp(&self, other: &Person) -> Option<Ordering> { Some(self.cmp(other)) } } impl Ord for Person { fn cmp(&self, other: &Person) -> Ordering { self.height.cmp(&other.height) } } impl PartialEq for Person { fn eq(&self, other: &Person) -> bool { self.height == other.height } }
You may also find it useful to use partial_cmp() on your types fields. Here is an example ofPersontypes who have a floating-pointheight` field that
is the only field to be used for sorting:
use std::cmp::Ordering; struct Person { id: u32, name: String, height: f64, } impl PartialOrd for Person { fn partial_cmp(&self, other: &Person) -> Option<Ordering> { self.height.partial_cmp(&other.height) } } impl PartialEq for Person { fn eq(&self, other: &Person) -> bool { self.height == other.height } }
let x : u32 = 0; let y : u32 = 1; assert_eq!(x < y, true); assert_eq!(x.lt(&y), true);
fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>This method returns an ordering between self and other values if one exists.
use std::cmp::Ordering; let result = 1.0.partial_cmp(&2.0); assert_eq!(result, Some(Ordering::Less)); let result = 1.0.partial_cmp(&1.0); assert_eq!(result, Some(Ordering::Equal)); let result = 2.0.partial_cmp(&1.0); assert_eq!(result, Some(Ordering::Greater));
When comparison is impossible:
fn main() { let result = std::f64::NAN.partial_cmp(&1.0); assert_eq!(result, None); }let result = std::f64::NAN.partial_cmp(&1.0); assert_eq!(result, None);
fn lt(&self, other: &Rhs) -> boolThis method tests less than (for self and other) and is used by the < operator.
let result = 1.0 < 2.0; assert_eq!(result, true); let result = 2.0 < 1.0; assert_eq!(result, false);
fn le(&self, other: &Rhs) -> boolThis method tests less than or equal to (for self and other) and is used by the <=
operator.
let result = 1.0 <= 2.0; assert_eq!(result, true); let result = 2.0 <= 2.0; assert_eq!(result, true);
fn gt(&self, other: &Rhs) -> boolThis method tests greater than (for self and other) and is used by the > operator.
let result = 1.0 > 2.0; assert_eq!(result, false); let result = 2.0 > 2.0; assert_eq!(result, false);
fn ge(&self, other: &Rhs) -> boolThis method tests greater than or equal to (for self and other) and is used by the >=
operator.
let result = 2.0 >= 1.0; assert_eq!(result, true); let result = 2.0 >= 2.0; assert_eq!(result, true);
impl<T> PartialOrd<Wrapping<T>> for Wrapping<T> where T: PartialOrd<T>impl<T> PartialOrd<NonZero<T>> for NonZero<T> where T: Zeroable + PartialOrd<T>impl<Ret> PartialOrd<fn() -> Ret> for fn() -> Retimpl<Ret> PartialOrd<extern fn() -> Ret> for extern fn() -> Retimpl<Ret> PartialOrd<unsafe fn() -> Ret> for unsafe fn() -> Retimpl<Ret> PartialOrd<unsafe extern fn() -> Ret> for unsafe extern fn() -> Retimpl<Ret, A> PartialOrd<fn(A) -> Ret> for fn(A) -> Retimpl<Ret, A> PartialOrd<extern fn(A) -> Ret> for extern fn(A) -> Retimpl<Ret, A> PartialOrd<unsafe fn(A) -> Ret> for unsafe fn(A) -> Retimpl<Ret, A> PartialOrd<unsafe extern fn(A) -> Ret> for unsafe extern fn(A) -> Retimpl<Ret, A, B> PartialOrd<fn(A, B) -> Ret> for fn(A, B) -> Retimpl<Ret, A, B> PartialOrd<extern fn(A, B) -> Ret> for extern fn(A, B) -> Retimpl<Ret, A, B> PartialOrd<unsafe fn(A, B) -> Ret> for unsafe fn(A, B) -> Retimpl<Ret, A, B> PartialOrd<unsafe extern fn(A, B) -> Ret> for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> PartialOrd<fn(A, B, C) -> Ret> for fn(A, B, C) -> Retimpl<Ret, A, B, C> PartialOrd<extern fn(A, B, C) -> Ret> for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> PartialOrd<unsafe fn(A, B, C) -> Ret> for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> PartialOrd<unsafe extern fn(A, B, C) -> Ret> for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> PartialOrd<fn(A, B, C, D) -> Ret> for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> PartialOrd<extern fn(A, B, C, D) -> Ret> for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> PartialOrd<unsafe fn(A, B, C, D) -> Ret> for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> PartialOrd<unsafe extern fn(A, B, C, D) -> Ret> for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> PartialOrd<fn(A, B, C, D, E) -> Ret> for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> PartialOrd<extern fn(A, B, C, D, E) -> Ret> for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> PartialOrd<unsafe fn(A, B, C, D, E) -> Ret> for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> PartialOrd<unsafe extern fn(A, B, C, D, E) -> Ret> for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> PartialOrd<fn(A, B, C, D, E, F) -> Ret> for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> PartialOrd<extern fn(A, B, C, D, E, F) -> Ret> for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> PartialOrd<unsafe fn(A, B, C, D, E, F) -> Ret> for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> PartialOrd<unsafe extern fn(A, B, C, D, E, F) -> Ret> for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialOrd<fn(A, B, C, D, E, F, G) -> Ret> for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialOrd<extern fn(A, B, C, D, E, F, G) -> Ret> for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialOrd<unsafe fn(A, B, C, D, E, F, G) -> Ret> for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> PartialOrd<unsafe extern fn(A, B, C, D, E, F, G) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialOrd<fn(A, B, C, D, E, F, G, H) -> Ret> for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialOrd<extern fn(A, B, C, D, E, F, G, H) -> Ret> for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialOrd<unsafe fn(A, B, C, D, E, F, G, H) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> PartialOrd<unsafe extern fn(A, B, C, D, E, F, G, H) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialOrd<fn(A, B, C, D, E, F, G, H, I) -> Ret> for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialOrd<extern fn(A, B, C, D, E, F, G, H, I) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialOrd<unsafe fn(A, B, C, D, E, F, G, H, I) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> PartialOrd<unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialOrd<fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialOrd<extern fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialOrd<unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> PartialOrd<unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialOrd<fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialOrd<extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialOrd<unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> PartialOrd<unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialOrd<fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialOrd<extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialOrd<unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> PartialOrd<unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Ret> for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<T> PartialOrd<*const T> for *const T where T: ?Sizedimpl<T> PartialOrd<*mut T> for *mut T where T: ?Sizedimpl<T> PartialOrd<PhantomData<T>> for PhantomData<T> where T: ?Sizedimpl PartialOrd<Ordering> for Orderingimpl PartialOrd<()> for ()impl PartialOrd<bool> for boolimpl PartialOrd<f32> for f32impl PartialOrd<f64> for f64impl PartialOrd<char> for charimpl PartialOrd<usize> for usizeimpl PartialOrd<u8> for u8impl PartialOrd<u16> for u16impl PartialOrd<u32> for u32impl PartialOrd<u64> for u64impl PartialOrd<isize> for isizeimpl PartialOrd<i8> for i8impl PartialOrd<i16> for i16impl PartialOrd<i32> for i32impl PartialOrd<i64> for i64impl<'a, 'b, A, B> PartialOrd<&'b B> for &'a A where A: PartialOrd<B> + ?Sized, B: ?Sizedimpl<'a, 'b, A, B> PartialOrd<&'b mut B> for &'a mut A where A: PartialOrd<B> + ?Sized, B: ?Sizedimpl<T> PartialOrd<[T; 0]> for [T; 0] where T: PartialOrd<T>impl<T> PartialOrd<[T; 1]> for [T; 1] where T: PartialOrd<T>impl<T> PartialOrd<[T; 2]> for [T; 2] where T: PartialOrd<T>impl<T> PartialOrd<[T; 3]> for [T; 3] where T: PartialOrd<T>impl<T> PartialOrd<[T; 4]> for [T; 4] where T: PartialOrd<T>impl<T> PartialOrd<[T; 5]> for [T; 5] where T: PartialOrd<T>impl<T> PartialOrd<[T; 6]> for [T; 6] where T: PartialOrd<T>impl<T> PartialOrd<[T; 7]> for [T; 7] where T: PartialOrd<T>impl<T> PartialOrd<[T; 8]> for [T; 8] where T: PartialOrd<T>impl<T> PartialOrd<[T; 9]> for [T; 9] where T: PartialOrd<T>impl<T> PartialOrd<[T; 10]> for [T; 10] where T: PartialOrd<T>impl<T> PartialOrd<[T; 11]> for [T; 11] where T: PartialOrd<T>impl<T> PartialOrd<[T; 12]> for [T; 12] where T: PartialOrd<T>impl<T> PartialOrd<[T; 13]> for [T; 13] where T: PartialOrd<T>impl<T> PartialOrd<[T; 14]> for [T; 14] where T: PartialOrd<T>impl<T> PartialOrd<[T; 15]> for [T; 15] where T: PartialOrd<T>impl<T> PartialOrd<[T; 16]> for [T; 16] where T: PartialOrd<T>impl<T> PartialOrd<[T; 17]> for [T; 17] where T: PartialOrd<T>impl<T> PartialOrd<[T; 18]> for [T; 18] where T: PartialOrd<T>impl<T> PartialOrd<[T; 19]> for [T; 19] where T: PartialOrd<T>impl<T> PartialOrd<[T; 20]> for [T; 20] where T: PartialOrd<T>impl<T> PartialOrd<[T; 21]> for [T; 21] where T: PartialOrd<T>impl<T> PartialOrd<[T; 22]> for [T; 22] where T: PartialOrd<T>impl<T> PartialOrd<[T; 23]> for [T; 23] where T: PartialOrd<T>impl<T> PartialOrd<[T; 24]> for [T; 24] where T: PartialOrd<T>impl<T> PartialOrd<[T; 25]> for [T; 25] where T: PartialOrd<T>impl<T> PartialOrd<[T; 26]> for [T; 26] where T: PartialOrd<T>impl<T> PartialOrd<[T; 27]> for [T; 27] where T: PartialOrd<T>impl<T> PartialOrd<[T; 28]> for [T; 28] where T: PartialOrd<T>impl<T> PartialOrd<[T; 29]> for [T; 29] where T: PartialOrd<T>impl<T> PartialOrd<[T; 30]> for [T; 30] where T: PartialOrd<T>impl<T> PartialOrd<[T; 31]> for [T; 31] where T: PartialOrd<T>impl<T> PartialOrd<[T; 32]> for [T; 32] where T: PartialOrd<T>impl<T> PartialOrd<Cell<T>> for Cell<T> where T: Copy + PartialOrd<T>impl<T> PartialOrd<RefCell<T>> for RefCell<T> where T: PartialOrd<T> + ?Sizedimpl<T> PartialOrd<Option<T>> for Option<T> where T: PartialOrd<T>impl<T, E> PartialOrd<Result<T, E>> for Result<T, E> where E: PartialOrd<E>, T: PartialOrd<T>impl<T> PartialOrd<[T]> for [T] where T: PartialOrd<T>impl PartialOrd<str> for strimpl PartialOrd<Error> for Errorimpl<A> PartialOrd<(A,)> for (A,) where A: PartialEq<A> + PartialOrd<A>impl<A, B> PartialOrd<(A, B)> for (A, B) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>impl<A, B, C> PartialOrd<(A, B, C)> for (A, B, C) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>impl<A, B, C, D> PartialOrd<(A, B, C, D)> for (A, B, C, D) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>impl<A, B, C, D, E> PartialOrd<(A, B, C, D, E)> for (A, B, C, D, E) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>impl<A, B, C, D, E, F> PartialOrd<(A, B, C, D, E, F)> for (A, B, C, D, E, F) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>impl<A, B, C, D, E, F, G> PartialOrd<(A, B, C, D, E, F, G)> for (A, B, C, D, E, F, G) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, G: PartialEq<G> + PartialOrd<G>impl<A, B, C, D, E, F, G, H> PartialOrd<(A, B, C, D, E, F, G, H)> for (A, B, C, D, E, F, G, H) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, G: PartialEq<G> + PartialOrd<G>, H: PartialEq<H> + PartialOrd<H>impl<A, B, C, D, E, F, G, H, I> PartialOrd<(A, B, C, D, E, F, G, H, I)> for (A, B, C, D, E, F, G, H, I) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, G: PartialEq<G> + PartialOrd<G>, H: PartialEq<H> + PartialOrd<H>, I: PartialEq<I> + PartialOrd<I>impl<A, B, C, D, E, F, G, H, I, J> PartialOrd<(A, B, C, D, E, F, G, H, I, J)> for (A, B, C, D, E, F, G, H, I, J) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, G: PartialEq<G> + PartialOrd<G>, H: PartialEq<H> + PartialOrd<H>, I: PartialEq<I> + PartialOrd<I>, J: PartialEq<J> + PartialOrd<J>impl<A, B, C, D, E, F, G, H, I, J, K> PartialOrd<(A, B, C, D, E, F, G, H, I, J, K)> for (A, B, C, D, E, F, G, H, I, J, K) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, G: PartialEq<G> + PartialOrd<G>, H: PartialEq<H> + PartialOrd<H>, I: PartialEq<I> + PartialOrd<I>, J: PartialEq<J> + PartialOrd<J>, K: PartialEq<K> + PartialOrd<K>impl<A, B, C, D, E, F, G, H, I, J, K, L> PartialOrd<(A, B, C, D, E, F, G, H, I, J, K, L)> for (A, B, C, D, E, F, G, H, I, J, K, L) where A: PartialEq<A> + PartialOrd<A>, B: PartialEq<B> + PartialOrd<B>, C: PartialEq<C> + PartialOrd<C>, D: PartialEq<D> + PartialOrd<D>, E: PartialEq<E> + PartialOrd<E>, F: PartialEq<F> + PartialOrd<F>, G: PartialEq<G> + PartialOrd<G>, H: PartialEq<H> + PartialOrd<H>, I: PartialEq<I> + PartialOrd<I>, J: PartialEq<J> + PartialOrd<J>, K: PartialEq<K> + PartialOrd<K>, L: PartialEq<L> + PartialOrd<L>impl<T> PartialOrd<Box<T>> for Box<T> where T: PartialOrd<T> + ?Sizedimpl<T> PartialOrd<Arc<T>> for Arc<T> where T: PartialOrd<T> + ?Sizedimpl<T> PartialOrd<Rc<T>> for Rc<T> where T: PartialOrd<T> + ?Sizedimpl<K, V> PartialOrd<BTreeMap<K, V>> for BTreeMap<K, V> where K: PartialOrd<K>, V: PartialOrd<V>impl<T> PartialOrd<BTreeSet<T>> for BTreeSet<T> where T: PartialOrd<T>impl<'a, B> PartialOrd<Cow<'a, B>> for Cow<'a, B> where B: PartialOrd<B> + ToOwned + ?Sizedimpl<E> PartialOrd<EnumSet<E>> for EnumSet<E> where E: PartialOrd<E>impl<A> PartialOrd<LinkedList<A>> for LinkedList<A> where A: PartialOrd<A>impl PartialOrd<String> for Stringimpl<T> PartialOrd<Vec<T>> for Vec<T> where T: PartialOrd<T>impl<A> PartialOrd<VecDeque<A>> for VecDeque<A> where A: PartialOrd<A>impl PartialOrd for CStringimpl PartialOrd for CStrimpl PartialOrd for OsStringimpl PartialOrd<str> for OsStringimpl PartialOrd for OsStrimpl PartialOrd<str> for OsStrimpl<'a, 'b> PartialOrd<OsStr> for OsStringimpl<'a, 'b> PartialOrd<OsString> for OsStrimpl<'a, 'b> PartialOrd<&'a OsStr> for OsStringimpl<'a, 'b> PartialOrd<OsString> for &'a OsStrimpl<'a, 'b> PartialOrd<OsStr> for Cow<'a, OsStr>impl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for OsStrimpl<'a, 'b> PartialOrd<&'b OsStr> for Cow<'a, OsStr>impl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for &'b OsStrimpl<'a, 'b> PartialOrd<OsString> for Cow<'a, OsStr>impl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for OsStringimpl PartialOrd for IpAddrimpl PartialOrd for Ipv4Addrimpl PartialOrd for Ipv6Addrimpl<'a> PartialOrd for Prefix<'a>impl<'a> PartialOrd for PrefixComponent<'a>impl<'a> PartialOrd for Component<'a>impl<'a> PartialOrd for Components<'a>impl PartialOrd for PathBufimpl PartialOrd for Pathimpl<'a, 'b> PartialOrd<Path> for PathBufimpl<'a, 'b> PartialOrd<PathBuf> for Pathimpl<'a, 'b> PartialOrd<&'a Path> for PathBufimpl<'a, 'b> PartialOrd<PathBuf> for &'a Pathimpl<'a, 'b> PartialOrd<Path> for Cow<'a, Path>impl<'a, 'b> PartialOrd<Cow<'a, Path>> for Pathimpl<'a, 'b> PartialOrd<&'b Path> for Cow<'a, Path>impl<'a, 'b> PartialOrd<Cow<'a, Path>> for &'b Pathimpl<'a, 'b> PartialOrd<PathBuf> for Cow<'a, Path>impl<'a, 'b> PartialOrd<Cow<'a, Path>> for PathBufimpl<'a, 'b> PartialOrd<OsStr> for PathBufimpl<'a, 'b> PartialOrd<PathBuf> for OsStrimpl<'a, 'b> PartialOrd<&'a OsStr> for PathBufimpl<'a, 'b> PartialOrd<PathBuf> for &'a OsStrimpl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for PathBufimpl<'a, 'b> PartialOrd<PathBuf> for Cow<'a, OsStr>impl<'a, 'b> PartialOrd<OsString> for PathBufimpl<'a, 'b> PartialOrd<PathBuf> for OsStringimpl<'a, 'b> PartialOrd<OsStr> for Pathimpl<'a, 'b> PartialOrd<Path> for OsStrimpl<'a, 'b> PartialOrd<&'a OsStr> for Pathimpl<'a, 'b> PartialOrd<Path> for &'a OsStrimpl<'a, 'b> PartialOrd<Cow<'a, OsStr>> for Pathimpl<'a, 'b> PartialOrd<Path> for Cow<'a, OsStr>impl<'a, 'b> PartialOrd<OsString> for Pathimpl<'a, 'b> PartialOrd<Path> for OsStringimpl<'a, 'b> PartialOrd<OsStr> for &'a Pathimpl<'a, 'b> PartialOrd<&'a Path> for OsStrimpl<'a, 'b> PartialOrd<Cow<'b, OsStr>> for &'a Pathimpl<'a, 'b> PartialOrd<&'a Path> for Cow<'b, OsStr>impl<'a, 'b> PartialOrd<OsString> for &'a Pathimpl<'a, 'b> PartialOrd<&'a Path> for OsStringimpl<'a, 'b> PartialOrd<OsStr> for Cow<'a, Path>impl<'a, 'b> PartialOrd<Cow<'a, Path>> for OsStrimpl<'a, 'b> PartialOrd<&'b OsStr> for Cow<'a, Path>impl<'a, 'b> PartialOrd<Cow<'a, Path>> for &'b OsStrimpl<'a, 'b> PartialOrd<OsString> for Cow<'a, Path>impl<'a, 'b> PartialOrd<Cow<'a, Path>> for OsStringimpl PartialOrd for Durationimpl PartialOrd for Instantimpl PartialOrd for SystemTimepub trait Eq: PartialEq<Self> { }
Trait for equality comparisons which are equivalence relations.
This means, that in addition to a == b and a != b being strict inverses, the equality must
be (for all a, b and c):
a == a;a == b implies b == a; anda == b and b == c implies a == c.This property cannot be checked by the compiler, and therefore Eq implies
PartialEq, and has no extra methods.
This trait can be used with #[derive]. When derived, because Eq has
no extra methods, it is only informing the compiler that this is an
equivalence relation rather than a partial equivalence relation. Note that
the derive strategy requires all fields are PartialEq, which isn't
always desired.
Eq?If you cannot use the derive strategy, specify that your type implements
Eq, which has no methods:
enum BookFormat { Paperback, Hardback, Ebook } struct Book { isbn: i32, format: BookFormat, } impl PartialEq for Book { fn eq(&self, other: &Book) -> bool { self.isbn == other.isbn } } impl Eq for Book {}
impl<T> Eq for Wrapping<T> where T: Eqimpl<T> Eq for NonZero<T> where T: Zeroable + Eqimpl<T> Eq for *const T where T: ?Sizedimpl<T> Eq for *mut T where T: ?Sizedimpl<Ret> Eq for fn() -> Retimpl<Ret> Eq for extern fn() -> Retimpl<Ret> Eq for unsafe fn() -> Retimpl<Ret> Eq for unsafe extern fn() -> Retimpl<Ret, A> Eq for fn(A) -> Retimpl<Ret, A> Eq for extern fn(A) -> Retimpl<Ret, A> Eq for unsafe fn(A) -> Retimpl<Ret, A> Eq for unsafe extern fn(A) -> Retimpl<Ret, A, B> Eq for fn(A, B) -> Retimpl<Ret, A, B> Eq for extern fn(A, B) -> Retimpl<Ret, A, B> Eq for unsafe fn(A, B) -> Retimpl<Ret, A, B> Eq for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> Eq for fn(A, B, C) -> Retimpl<Ret, A, B, C> Eq for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> Eq for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> Eq for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> Eq for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Eq for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Eq for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Eq for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> Eq for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Eq for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Eq for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Eq for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> Eq for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Eq for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Eq for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Eq for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> Eq for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Eq for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Eq for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Eq for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Eq for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Eq for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Eq for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Eq for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Eq for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Eq for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Eq for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Eq for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Eq for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Eq for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Eq for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Eq for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Eq for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Eq for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Eq for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Eq for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Eq for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Eq for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Eq for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Eq for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<T> Eq for PhantomData<T> where T: ?Sizedimpl Eq for RangeFullimpl<Idx> Eq for Range<Idx> where Idx: Eqimpl<Idx> Eq for RangeFrom<Idx> where Idx: Eqimpl<Idx> Eq for RangeTo<Idx> where Idx: Eqimpl<Idx> Eq for RangeInclusive<Idx> where Idx: Eqimpl<Idx> Eq for RangeToInclusive<Idx> where Idx: Eqimpl Eq for Orderingimpl Eq for ()impl Eq for boolimpl Eq for charimpl Eq for usizeimpl Eq for u8impl Eq for u16impl Eq for u32impl Eq for u64impl Eq for isizeimpl Eq for i8impl Eq for i16impl Eq for i32impl Eq for i64impl<'a, A> Eq for &'a A where A: Eq + ?Sizedimpl<'a, A> Eq for &'a mut A where A: Eq + ?Sizedimpl Eq for TypeIdimpl<T> Eq for [T; 0] where T: Eqimpl<T> Eq for [T; 1] where T: Eqimpl<T> Eq for [T; 2] where T: Eqimpl<T> Eq for [T; 3] where T: Eqimpl<T> Eq for [T; 4] where T: Eqimpl<T> Eq for [T; 5] where T: Eqimpl<T> Eq for [T; 6] where T: Eqimpl<T> Eq for [T; 7] where T: Eqimpl<T> Eq for [T; 8] where T: Eqimpl<T> Eq for [T; 9] where T: Eqimpl<T> Eq for [T; 10] where T: Eqimpl<T> Eq for [T; 11] where T: Eqimpl<T> Eq for [T; 12] where T: Eqimpl<T> Eq for [T; 13] where T: Eqimpl<T> Eq for [T; 14] where T: Eqimpl<T> Eq for [T; 15] where T: Eqimpl<T> Eq for [T; 16] where T: Eqimpl<T> Eq for [T; 17] where T: Eqimpl<T> Eq for [T; 18] where T: Eqimpl<T> Eq for [T; 19] where T: Eqimpl<T> Eq for [T; 20] where T: Eqimpl<T> Eq for [T; 21] where T: Eqimpl<T> Eq for [T; 22] where T: Eqimpl<T> Eq for [T; 23] where T: Eqimpl<T> Eq for [T; 24] where T: Eqimpl<T> Eq for [T; 25] where T: Eqimpl<T> Eq for [T; 26] where T: Eqimpl<T> Eq for [T; 27] where T: Eqimpl<T> Eq for [T; 28] where T: Eqimpl<T> Eq for [T; 29] where T: Eqimpl<T> Eq for [T; 30] where T: Eqimpl<T> Eq for [T; 31] where T: Eqimpl<T> Eq for [T; 32] where T: Eqimpl<T> Eq for Cell<T> where T: Copy + Eqimpl Eq for BorrowStateimpl<T> Eq for RefCell<T> where T: Eq + ?Sizedimpl<T> Eq for Option<T> where T: Eqimpl<T, E> Eq for Result<T, E> where E: Eq, T: Eqimpl<T> Eq for [T] where T: Eqimpl Eq for SearchStepimpl Eq for Utf8Errorimpl Eq for strimpl Eq for Errorimpl<A> Eq for (A,) where A: Eqimpl<A, B> Eq for (A, B) where A: Eq, B: Eqimpl<A, B, C> Eq for (A, B, C) where A: Eq, B: Eq, C: Eqimpl<A, B, C, D> Eq for (A, B, C, D) where A: Eq, B: Eq, C: Eq, D: Eqimpl<A, B, C, D, E> Eq for (A, B, C, D, E) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eqimpl<A, B, C, D, E, F> Eq for (A, B, C, D, E, F) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eqimpl<A, B, C, D, E, F, G> Eq for (A, B, C, D, E, F, G) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eq, G: Eqimpl<A, B, C, D, E, F, G, H> Eq for (A, B, C, D, E, F, G, H) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eq, G: Eq, H: Eqimpl<A, B, C, D, E, F, G, H, I> Eq for (A, B, C, D, E, F, G, H, I) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eq, G: Eq, H: Eq, I: Eqimpl<A, B, C, D, E, F, G, H, I, J> Eq for (A, B, C, D, E, F, G, H, I, J) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eq, G: Eq, H: Eq, I: Eq, J: Eqimpl<A, B, C, D, E, F, G, H, I, J, K> Eq for (A, B, C, D, E, F, G, H, I, J, K) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eq, G: Eq, H: Eq, I: Eq, J: Eq, K: Eqimpl<A, B, C, D, E, F, G, H, I, J, K, L> Eq for (A, B, C, D, E, F, G, H, I, J, K, L) where A: Eq, B: Eq, C: Eq, D: Eq, E: Eq, F: Eq, G: Eq, H: Eq, I: Eq, J: Eq, K: Eq, L: Eqimpl<T> Eq for Box<T> where T: Eq + ?Sizedimpl<T> Eq for Arc<T> where T: Eq + ?Sizedimpl<T> Eq for Rc<T> where T: Eq + ?Sizedimpl<K, V> Eq for BTreeMap<K, V> where K: Eq, V: Eqimpl<T> Eq for BTreeSet<T> where T: Eqimpl<'a, B> Eq for Cow<'a, B> where B: Eq + ToOwned + ?Sizedimpl<E> Eq for EnumSet<E> where E: Eqimpl<A> Eq for LinkedList<A> where A: Eqimpl Eq for Stringimpl Eq for ParseErrorimpl<T> Eq for Vec<T> where T: Eqimpl<A> Eq for VecDeque<A> where A: Eqimpl<T> Eq for Bound<T> where T: Eqimpl Eq for DecodeUtf16Errorimpl Eq for LocalKeyStateimpl<K, V, S> Eq for HashMap<K, V, S> where K: Eq + Hash, V: Eq, S: BuildHasherimpl<T, S> Eq for HashSet<T, S> where T: Eq + Hash, S: BuildHasherimpl Eq for VarErrorimpl Eq for CStringimpl Eq for CStrimpl Eq for OsStringimpl Eq for OsStrimpl Eq for Permissionsimpl Eq for FileTypeimpl Eq for ErrorKindimpl Eq for SeekFromimpl Eq for IpAddrimpl Eq for Ipv6MulticastScopeimpl Eq for Ipv4Addrimpl Eq for Ipv6Addrimpl Eq for SocketAddrimpl Eq for SocketAddrV4impl Eq for SocketAddrV6impl<'a> Eq for Prefix<'a>impl<'a> Eq for PrefixComponent<'a>impl<'a> Eq for Component<'a>impl<'a> Eq for Components<'a>impl Eq for PathBufimpl Eq for StripPrefixErrorimpl Eq for Pathimpl Eq for Outputimpl Eq for ExitStatusimpl<T: Eq> Eq for SendError<T>impl Eq for RecvErrorimpl Eq for TryRecvErrorimpl Eq for RecvTimeoutErrorimpl<T: Eq> Eq for TrySendError<T>impl Eq for WaitTimeoutResultimpl Eq for Durationimpl Eq for Instantimpl Eq for SystemTimepub trait Ord: Eq + PartialOrd<Self> { fn cmp(&self, other: &Self) -> Ordering; }
Trait for types that form a total order.
An order is a total order if it is (for all a, b and c):
a < b, a == b or a > b is true; anda < b and b < c implies a < c. The same must hold for both == and >.This trait can be used with #[derive]. When derived, it will produce a lexicographic
ordering based on the top-to-bottom declaration order of the struct's members.
Ord?Ord requires that the type also be PartialOrd and Eq (which requires PartialEq).
Then you must define an implementation for cmp(). You may find it useful to use
cmp() on your type's fields.
Here's an example where you want to sort people by height only, disregarding id
and name:
use std::cmp::Ordering; #[derive(Eq)] struct Person { id: u32, name: String, height: u32, } impl Ord for Person { fn cmp(&self, other: &Person) -> Ordering { self.height.cmp(&other.height) } } impl PartialOrd for Person { fn partial_cmp(&self, other: &Person) -> Option<Ordering> { Some(self.cmp(other)) } } impl PartialEq for Person { fn eq(&self, other: &Person) -> bool { self.height == other.height } }
fn cmp(&self, other: &Self) -> OrderingThis method returns an Ordering between self and other.
By convention, self.cmp(&other) returns the ordering matching the expression
self <operator> other if true.
use std::cmp::Ordering; assert_eq!(5.cmp(&10), Ordering::Less); assert_eq!(10.cmp(&5), Ordering::Greater); assert_eq!(5.cmp(&5), Ordering::Equal);
impl<T> Ord for Wrapping<T> where T: Ordimpl<T> Ord for NonZero<T> where T: Zeroable + Ordimpl<Ret> Ord for fn() -> Retimpl<Ret> Ord for extern fn() -> Retimpl<Ret> Ord for unsafe fn() -> Retimpl<Ret> Ord for unsafe extern fn() -> Retimpl<Ret, A> Ord for fn(A) -> Retimpl<Ret, A> Ord for extern fn(A) -> Retimpl<Ret, A> Ord for unsafe fn(A) -> Retimpl<Ret, A> Ord for unsafe extern fn(A) -> Retimpl<Ret, A, B> Ord for fn(A, B) -> Retimpl<Ret, A, B> Ord for extern fn(A, B) -> Retimpl<Ret, A, B> Ord for unsafe fn(A, B) -> Retimpl<Ret, A, B> Ord for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> Ord for fn(A, B, C) -> Retimpl<Ret, A, B, C> Ord for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> Ord for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> Ord for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> Ord for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Ord for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Ord for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Ord for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> Ord for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Ord for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Ord for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Ord for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> Ord for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Ord for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Ord for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Ord for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> Ord for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Ord for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Ord for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Ord for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Ord for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Ord for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Ord for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Ord for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Ord for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Ord for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Ord for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Ord for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Ord for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Ord for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Ord for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Ord for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Ord for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Ord for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Ord for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Ord for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Ord for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Ord for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Ord for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Ord for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<T> Ord for *const T where T: ?Sizedimpl<T> Ord for *mut T where T: ?Sizedimpl<T> Ord for PhantomData<T> where T: ?Sizedimpl Ord for Orderingimpl Ord for ()impl Ord for boolimpl Ord for charimpl Ord for usizeimpl Ord for u8impl Ord for u16impl Ord for u32impl Ord for u64impl Ord for isizeimpl Ord for i8impl Ord for i16impl Ord for i32impl Ord for i64impl<'a, A> Ord for &'a A where A: Ord + ?Sizedimpl<'a, A> Ord for &'a mut A where A: Ord + ?Sizedimpl<T> Ord for [T; 0] where T: Ordimpl<T> Ord for [T; 1] where T: Ordimpl<T> Ord for [T; 2] where T: Ordimpl<T> Ord for [T; 3] where T: Ordimpl<T> Ord for [T; 4] where T: Ordimpl<T> Ord for [T; 5] where T: Ordimpl<T> Ord for [T; 6] where T: Ordimpl<T> Ord for [T; 7] where T: Ordimpl<T> Ord for [T; 8] where T: Ordimpl<T> Ord for [T; 9] where T: Ordimpl<T> Ord for [T; 10] where T: Ordimpl<T> Ord for [T; 11] where T: Ordimpl<T> Ord for [T; 12] where T: Ordimpl<T> Ord for [T; 13] where T: Ordimpl<T> Ord for [T; 14] where T: Ordimpl<T> Ord for [T; 15] where T: Ordimpl<T> Ord for [T; 16] where T: Ordimpl<T> Ord for [T; 17] where T: Ordimpl<T> Ord for [T; 18] where T: Ordimpl<T> Ord for [T; 19] where T: Ordimpl<T> Ord for [T; 20] where T: Ordimpl<T> Ord for [T; 21] where T: Ordimpl<T> Ord for [T; 22] where T: Ordimpl<T> Ord for [T; 23] where T: Ordimpl<T> Ord for [T; 24] where T: Ordimpl<T> Ord for [T; 25] where T: Ordimpl<T> Ord for [T; 26] where T: Ordimpl<T> Ord for [T; 27] where T: Ordimpl<T> Ord for [T; 28] where T: Ordimpl<T> Ord for [T; 29] where T: Ordimpl<T> Ord for [T; 30] where T: Ordimpl<T> Ord for [T; 31] where T: Ordimpl<T> Ord for [T; 32] where T: Ordimpl<T> Ord for Cell<T> where T: Copy + Ordimpl<T> Ord for RefCell<T> where T: Ord + ?Sizedimpl<T> Ord for Option<T> where T: Ordimpl<T, E> Ord for Result<T, E> where E: Ord, T: Ordimpl<T> Ord for [T] where T: Ordimpl Ord for strimpl Ord for Errorimpl<A> Ord for (A,) where A: Ordimpl<A, B> Ord for (A, B) where A: Ord, B: Ordimpl<A, B, C> Ord for (A, B, C) where A: Ord, B: Ord, C: Ordimpl<A, B, C, D> Ord for (A, B, C, D) where A: Ord, B: Ord, C: Ord, D: Ordimpl<A, B, C, D, E> Ord for (A, B, C, D, E) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ordimpl<A, B, C, D, E, F> Ord for (A, B, C, D, E, F) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ordimpl<A, B, C, D, E, F, G> Ord for (A, B, C, D, E, F, G) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ord, G: Ordimpl<A, B, C, D, E, F, G, H> Ord for (A, B, C, D, E, F, G, H) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ord, G: Ord, H: Ordimpl<A, B, C, D, E, F, G, H, I> Ord for (A, B, C, D, E, F, G, H, I) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ord, G: Ord, H: Ord, I: Ordimpl<A, B, C, D, E, F, G, H, I, J> Ord for (A, B, C, D, E, F, G, H, I, J) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ord, G: Ord, H: Ord, I: Ord, J: Ordimpl<A, B, C, D, E, F, G, H, I, J, K> Ord for (A, B, C, D, E, F, G, H, I, J, K) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ord, G: Ord, H: Ord, I: Ord, J: Ord, K: Ordimpl<A, B, C, D, E, F, G, H, I, J, K, L> Ord for (A, B, C, D, E, F, G, H, I, J, K, L) where A: Ord, B: Ord, C: Ord, D: Ord, E: Ord, F: Ord, G: Ord, H: Ord, I: Ord, J: Ord, K: Ord, L: Ordimpl<T> Ord for Box<T> where T: Ord + ?Sizedimpl<T> Ord for Arc<T> where T: Ord + ?Sizedimpl<T> Ord for Rc<T> where T: Ord + ?Sizedimpl<K, V> Ord for BTreeMap<K, V> where K: Ord, V: Ordimpl<T> Ord for BTreeSet<T> where T: Ordimpl<'a, B> Ord for Cow<'a, B> where B: Ord + ToOwned + ?Sizedimpl<E> Ord for EnumSet<E> where E: Ordimpl<A> Ord for LinkedList<A> where A: Ordimpl Ord for Stringimpl<T> Ord for Vec<T> where T: Ordimpl<A> Ord for VecDeque<A> where A: Ordimpl Ord for CStringimpl Ord for CStrimpl Ord for OsStringimpl Ord for OsStrimpl Ord for IpAddrimpl Ord for Ipv4Addrimpl Ord for Ipv6Addrimpl<'a> Ord for Prefix<'a>impl<'a> Ord for PrefixComponent<'a>impl<'a> Ord for Component<'a>impl<'a> Ord for Components<'a>impl Ord for PathBufimpl Ord for Pathimpl Ord for Durationimpl Ord for Instantimpl Ord for SystemTimepub trait Write {
fn write(&mut self, buf: &[u8]) -> Result<usize>;
fn flush(&mut self) -> Result<()>;
fn write_all(&mut self, buf: &[u8]) -> Result<()> { ... }
fn write_fmt(&mut self, fmt: Arguments) -> Result<()> { ... }
fn by_ref(&mut self) -> &mut Self where Self: Sized { ... }
}A trait for objects which are byte-oriented sinks.
Implementors of the Write trait are sometimes called 'writers'.
Writers are defined by two required methods, write() and flush():
The write() method will attempt to write some data into the object,
returning how many bytes were successfully written.
The flush() method is useful for adaptors and explicit buffers
themselves for ensuring that all buffered data has been pushed out to the
'true sink'.
Writers are intended to be composable with one another. Many implementors
throughout std::io take and provide types which implement the Write
trait.
use std::io::prelude::*; use std::fs::File; let mut buffer = try!(File::create("foo.txt")); try!(buffer.write(b"some bytes"));
fn write(&mut self, buf: &[u8]) -> Result<usize>Write a buffer into this object, returning how many bytes were written.
This function will attempt to write the entire contents of buf, but
the entire write may not succeed, or the write may also generate an
error. A call to write represents at most one attempt to write to
any wrapped object.
Calls to write are not guaranteed to block waiting for data to be
written, and a write which would otherwise block can be indicated through
an Err variant.
If the return value is Ok(n) then it must be guaranteed that
0 <= n <= buf.len(). A return value of 0 typically means that the
underlying object is no longer able to accept bytes and will likely not
be able to in the future as well, or that the buffer provided is empty.
Each call to write may generate an I/O error indicating that the
operation could not be completed. If an error is returned then no bytes
in the buffer were written to this writer.
It is not considered an error if the entire buffer could not be written to this writer.
use std::io::prelude::*; use std::fs::File; let mut buffer = try!(File::create("foo.txt")); try!(buffer.write(b"some bytes"));
fn flush(&mut self) -> Result<()>Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
It is considered an error if not all bytes could be written due to I/O errors or EOF being reached.
use std::io::prelude::*; use std::io::BufWriter; use std::fs::File; let mut buffer = BufWriter::new(try!(File::create("foo.txt"))); try!(buffer.write(b"some bytes")); try!(buffer.flush());
fn write_all(&mut self, buf: &[u8]) -> Result<()>Attempts to write an entire buffer into this write.
This method will continuously call write while there is more data to
write. This method will not return until the entire buffer has been
successfully written or an error occurs. The first error generated from
this method will be returned.
This function will return the first error that write returns.
use std::io::prelude::*; use std::fs::File; let mut buffer = try!(File::create("foo.txt")); try!(buffer.write_all(b"some bytes"));
fn write_fmt(&mut self, fmt: Arguments) -> Result<()>Writes a formatted string into this writer, returning any error encountered.
This method is primarily used to interface with the
format_args! macro, but it is rare that this should
explicitly be called. The write! macro should be favored to
invoke this method instead.
This function internally uses the write_all method on
this trait and hence will continuously write data so long as no errors
are received. This also means that partial writes are not indicated in
this signature.
This function will return any I/O error reported while formatting.
use std::io::prelude::*; use std::fs::File; let mut buffer = try!(File::create("foo.txt")); // this call try!(write!(buffer, "{:.*}", 2, 1.234567)); // turns into this: try!(buffer.write_fmt(format_args!("{:.*}", 2, 1.234567)));
fn by_ref(&mut self) -> &mut Self where Self: SizedCreates a "by reference" adaptor for this instance of Write.
The returned adaptor also implements Write and will simply borrow this
current writer.
use std::io::Write; use std::fs::File; let mut buffer = try!(File::create("foo.txt")); let reference = buffer.by_ref(); // we can use reference just like our original buffer try!(reference.write_all(b"some bytes"));
impl Write for Fileimpl<'a> Write for &'a Fileimpl<W: Write> Write for BufWriter<W>impl<W: Write> Write for LineWriter<W>impl<'a> Write for Cursor<&'a mut [u8]>impl Write for Cursor<Vec<u8>>impl Write for Cursor<Box<[u8]>>impl<'a, W: Write + ?Sized> Write for &'a mut Wimpl<W: Write + ?Sized> Write for Box<W>impl<'a> Write for &'a mut [u8]impl Write for Vec<u8>impl Write for Sinkimpl Write for Stdoutimpl<'a> Write for StdoutLock<'a>impl Write for Stderrimpl<'a> Write for StderrLock<'a>impl Write for TcpStreamimpl<'a> Write for &'a TcpStreamimpl Write for UnixStreamimpl<'a> Write for &'a UnixStreamimpl Write for ChildStdinfn find<P>(&mut self, predicate: P) -> Option<Self::Item> where P: FnMut(&Self::Item) -> boolSearches for an element of an iterator that satisfies a predicate. Read more
A hash map implementation which uses linear probing with Robin Hood bucket stealing.
The hashes are all keyed by the thread-local random number generator on creation by default. This means that the ordering of the keys is randomized, but makes the tables more resistant to denial-of-service attacks (Hash DoS). No guarantees are made to the quality of the random data. The implementation uses the best available random data from your platform at the time of creation. This behavior can be overridden with one of the constructors.
It is required that the keys implement the Eq and Hash traits, although
this can frequently be achieved by using #[derive(PartialEq, Eq, Hash)].
If you implement these yourself, it is important that the following
property holds:
k1 == k2 -> hash(k1) == hash(k2)
In other words, if two keys are equal, their hashes must be equal.
It is a logic error for a key to be modified in such a way that the key's
hash, as determined by the Hash trait, or its equality, as determined by
the Eq trait, changes while it is in the map. This is normally only
possible through Cell, RefCell, global state, I/O, or unsafe code.
Relevant papers/articles:
use std::collections::HashMap; // type inference lets us omit an explicit type signature (which // would be `HashMap<&str, &str>` in this example). let mut book_reviews = HashMap::new(); // review some books. book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book."); book_reviews.insert("Grimms' Fairy Tales", "Masterpiece."); book_reviews.insert("Pride and Prejudice", "Very enjoyable."); book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot."); // check for a specific one. if !book_reviews.contains_key("Les Misérables") { println!("We've got {} reviews, but Les Misérables ain't one.", book_reviews.len()); } // oops, this review has a lot of spelling mistakes, let's delete it. book_reviews.remove("The Adventures of Sherlock Holmes"); // look up the values associated with some keys. let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"]; for book in &to_find { match book_reviews.get(book) { Some(review) => println!("{}: {}", book, review), None => println!("{} is unreviewed.", book) } } // iterate over everything. for (book, review) in &book_reviews { println!("{}: \"{}\"", book, review); }
HashMap also implements an Entry API, which allows
for more complex methods of getting, setting, updating and removing keys and
their values:
use std::collections::HashMap; // type inference lets us omit an explicit type signature (which // would be `HashMap<&str, u8>` in this example). let mut player_stats = HashMap::new(); fn random_stat_buff() -> u8 { // could actually return some random value here - let's just return // some fixed value for now 42 } // insert a key only if it doesn't already exist player_stats.entry("health").or_insert(100); // insert a key using a function that provides a new value only if it // doesn't already exist player_stats.entry("defence").or_insert_with(random_stat_buff); // update a key, guarding against the key possibly not being set let stat = player_stats.entry("attack").or_insert(100); *stat += random_stat_buff();
The easiest way to use HashMap with a custom type as key is to derive Eq and Hash.
We must also derive PartialEq.
use std::collections::HashMap; #[derive(Hash, Eq, PartialEq, Debug)] struct Viking { name: String, country: String, } impl Viking { /// Create a new Viking. fn new(name: &str, country: &str) -> Viking { Viking { name: name.to_string(), country: country.to_string() } } } // Use a HashMap to store the vikings' health points. let mut vikings = HashMap::new(); vikings.insert(Viking::new("Einar", "Norway"), 25); vikings.insert(Viking::new("Olaf", "Denmark"), 24); vikings.insert(Viking::new("Harald", "Iceland"), 12); // Use derived implementation to print the status of the vikings. for (viking, health) in &vikings { println!("{:?} has {} hp", viking, health); }
fn new() -> HashMap<K, V, RandomState>Creates an empty HashMap.
use std::collections::HashMap; let mut map: HashMap<&str, isize> = HashMap::new();
fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState>Creates an empty hash map with the given initial capacity.
use std::collections::HashMap; let mut map: HashMap<&str, isize> = HashMap::with_capacity(10);
fn index(&self, index: &Q) -> &VThe method for the indexing (Foo[Bar]) operation
fn len(&self) -> usizeReturns the number of elements in the map.
use std::collections::HashMap; let mut a = HashMap::new(); assert_eq!(a.len(), 0); a.insert(1, "a"); assert_eq!(a.len(), 1);
fn iter(&self) -> Iter<K, V>An iterator visiting all key-value pairs in arbitrary order.
Iterator element type is (&'a K, &'a V).
use std::collections::HashMap; let mut map = HashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); for (key, val) in map.iter() { println!("key: {} val: {}", key, val); }
fn iter_mut(&mut self) -> IterMut<K, V>An iterator visiting all key-value pairs in arbitrary order,
with mutable references to the values.
Iterator element type is (&'a K, &'a mut V).
use std::collections::HashMap; let mut map = HashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); // Update all values for (_, val) in map.iter_mut() { *val *= 2; } for (key, val) in &map { println!("key: {} val: {}", key, val); }
fn keys(&self) -> Keys<K, V>An iterator visiting all keys in arbitrary order.
Iterator element type is &'a K.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); for key in map.keys() { println!("{}", key); }
fn values(&self) -> Values<K, V>An iterator visiting all values in arbitrary order.
Iterator element type is &'a V.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); for val in map.values() { println!("{}", val); }
fn values_mut(&mut self) -> ValuesMut<K, V>An iterator visiting all values mutably in arbitrary order.
Iterator element type is &'a mut V.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); for val in map.values_mut() { *val = *val + 10; } for val in map.values() { print!("{}", val); }
fn is_empty(&self) -> boolReturns true if the map contains no elements.
use std::collections::HashMap; let mut a = HashMap::new(); assert!(a.is_empty()); a.insert(1, "a"); assert!(!a.is_empty());
fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool where K: Borrow<Q>, Q: Hash + EqReturns true if the map contains a value for the specified key.
The key may be any borrowed form of the map's key type, but
Hash and Eq on the borrowed form must match those for
the key type.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert(1, "a"); assert_eq!(map.contains_key(&1), true); assert_eq!(map.contains_key(&2), false);
fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> where K: Borrow<Q>, Q: Hash + EqReturns a reference to the value corresponding to the key.
The key may be any borrowed form of the map's key type, but
Hash and Eq on the borrowed form must match those for
the key type.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert(1, "a"); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.get(&2), None);
fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Hash + EqReturns a mutable reference to the value corresponding to the key.
The key may be any borrowed form of the map's key type, but
Hash and Eq on the borrowed form must match those for
the key type.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert(1, "a"); if let Some(x) = map.get_mut(&1) { *x = "b"; } assert_eq!(map[&1], "b");
fn entry(&mut self, key: K) -> Entry<K, V>Gets the given key's corresponding entry in the map for in-place manipulation.
use std::collections::HashMap; let mut letters = HashMap::new(); for ch in "a short treatise on fungi".chars() { let counter = letters.entry(ch).or_insert(0); *counter += 1; } assert_eq!(letters[&'s'], 2); assert_eq!(letters[&'t'], 3); assert_eq!(letters[&'u'], 1); assert_eq!(letters.get(&'y'), None);
fn drain(&mut self) -> Drain<K, V>Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
use std::collections::HashMap; let mut a = HashMap::new(); a.insert(1, "a"); a.insert(2, "b"); for (k, v) in a.drain().take(1) { assert!(k == 1 || k == 2); assert!(v == "a" || v == "b"); } assert!(a.is_empty());
fn clear(&mut self)Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
use std::collections::HashMap; let mut a = HashMap::new(); a.insert(1, "a"); a.clear(); assert!(a.is_empty());
fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T)Extends a collection with the contents of an iterator. Read more
fn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T)Extends a collection with the contents of an iterator. Read more
fn insert(&mut self, k: K, v: V) -> Option<V>Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old
value is returned. The key is not updated, though; this matters for
types that can be == without being identical. See the module-level
documentation for more.
use std::collections::HashMap; let mut map = HashMap::new(); assert_eq!(map.insert(37, "a"), None); assert_eq!(map.is_empty(), false); map.insert(37, "b"); assert_eq!(map.insert(37, "c"), Some("b")); assert_eq!(map[&37], "c");
fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> where K: Borrow<Q>, Q: Hash + EqRemoves a key from the map, returning the value at the key if the key was previously in the map.
The key may be any borrowed form of the map's key type, but
Hash and Eq on the borrowed form must match those for
the key type.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert(1, "a"); assert_eq!(map.remove(&1), Some("a")); assert_eq!(map.remove(&1), None);
fn from_iter<T: IntoIterator<Item=(K, V)>>(iter: T) -> HashMap<K, V, S>Creates a value from an iterator. Read more
fn capacity(&self) -> usizeReturns the number of elements the map can hold without reallocating.
This number is a lower bound; the HashMap<K, V> might be able to hold
more, but is guaranteed to be able to hold at least this many.
use std::collections::HashMap; let map: HashMap<isize, isize> = HashMap::with_capacity(100); assert!(map.capacity() >= 100);
fn reserve(&mut self, additional: usize)Reserves capacity for at least additional more elements to be inserted
in the HashMap. The collection may reserve more space to avoid
frequent reallocations.
Panics if the new allocation size overflows usize.
use std::collections::HashMap; let mut map: HashMap<&str, isize> = HashMap::new(); map.reserve(10);
fn shrink_to_fit(&mut self)Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
use std::collections::HashMap; let mut map: HashMap<isize, isize> = HashMap::with_capacity(100); map.insert(1, 2); map.insert(3, 4); assert!(map.capacity() >= 100); map.shrink_to_fit(); assert!(map.capacity() >= 2);
fn clone_from(&mut self, source: &Self)Performs copy-assignment from source. Read more
fn eq(&self, other: &HashMap<K, V, S>) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn with_hasher(hash_builder: S) -> HashMap<K, V, S>Creates an empty hashmap which will use the given hash builder to hash keys.
The created map has the default initial capacity.
Warning: hash_builder is normally randomly generated, and
is designed to allow HashMaps to be resistant to attacks that
cause many collisions and very poor performance. Setting it
manually using this function can expose a DoS attack vector.
use std::collections::HashMap; use std::collections::hash_map::RandomState; let s = RandomState::new(); let mut map = HashMap::with_hasher(s); map.insert(1, 2);
fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S>Creates an empty HashMap with space for at least capacity
elements, using hasher to hash the keys.
Warning: hasher is normally randomly generated, and
is designed to allow HashMaps to be resistant to attacks that
cause many collisions and very poor performance. Setting it
manually using this function can expose a DoS attack vector.
use std::collections::HashMap; use std::collections::hash_map::RandomState; let s = RandomState::new(); let mut map = HashMap::with_capacity_and_hasher(10, s); map.insert(1, 2);
fn hasher(&self) -> &SReturns a reference to the map's hasher.
impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher[src]type Item = (&'a K, &'a V)The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V>Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, K, V>Creates an iterator from a value. Read more
impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher[src]type Item = (&'a K, &'a mut V)The type of the elements being iterated over.
type IntoIter = IterMut<'a, K, V>Which kind of iterator are we turning this into?
fn into_iter(self) -> IterMut<'a, K, V>Creates an iterator from a value. Read more
impl<K, V, S> IntoIterator for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher[src]type Item = (K, V)The type of the elements being iterated over.
type IntoIter = IntoIter<K, V>Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<K, V>Creates a consuming iterator, that is, one that moves each key-value pair out of the map in arbitrary order. The map cannot be used after calling this.
use std::collections::HashMap; let mut map = HashMap::new(); map.insert("a", 1); map.insert("b", 2); map.insert("c", 3); // Not possible with .iter() let vec: Vec<(&str, isize)> = map.into_iter().collect();
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher + Default[src]impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher[src]fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T)Extends a collection with the contents of an iterator. Read more
impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S> where K: Eq + Hash + Copy, V: Copy, S: BuildHasherfn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T)Extends a collection with the contents of an iterator. Read more
The Option type. See the module level documentation for more.
fn is_some(&self) -> boolReturns true if the option is a Some value
let x: Option<u32> = Some(2); assert_eq!(x.is_some(), true); let x: Option<u32> = None; assert_eq!(x.is_some(), false);
fn is_none(&self) -> boolReturns true if the option is a None value
let x: Option<u32> = Some(2); assert_eq!(x.is_none(), false); let x: Option<u32> = None; assert_eq!(x.is_none(), true);
fn as_ref(&self) -> Option<&T>Converts from Option<T> to Option<&T>
Convert an Option<String> into an Option<usize>, preserving the original.
The map method takes the self argument by value, consuming the original,
so this technique uses as_ref to first take an Option to a reference
to the value inside the original.
let num_as_str: Option<String> = Some("10".to_string()); // First, cast `Option<String>` to `Option<&String>` with `as_ref`, // then consume *that* with `map`, leaving `num_as_str` on the stack. let num_as_int: Option<usize> = num_as_str.as_ref().map(|n| n.len()); println!("still can print num_as_str: {:?}", num_as_str);
fn as_mut(&mut self) -> Option<&mut T>Converts from Option<T> to Option<&mut T>
let mut x = Some(2); match x.as_mut() { Some(v) => *v = 42, None => {}, } assert_eq!(x, Some(42));
fn cloned(self) -> Option<T>Maps an Option<&T> to an Option<T> by cloning the contents of the
option.
fn iter(&self) -> Iter<T>Returns an iterator over the possibly contained value.
let x = Some(4); assert_eq!(x.iter().next(), Some(&4)); let x: Option<u32> = None; assert_eq!(x.iter().next(), None);
fn iter_mut(&mut self) -> IterMut<T>Returns a mutable iterator over the possibly contained value.
let mut x = Some(4); match x.iter_mut().next() { Some(v) => *v = 42, None => {}, } assert_eq!(x, Some(42)); let mut x: Option<u32> = None; assert_eq!(x.iter_mut().next(), None);
fn unwrap(self) -> TMoves the value v out of the Option<T> if it is Some(v).
Panics if the self value equals None.
In general, because this function may panic, its use is discouraged.
Instead, prefer to use pattern matching and handle the None
case explicitly.
let x = Some("air"); assert_eq!(x.unwrap(), "air");fn main() { let x: Option<&str> = None; assert_eq!(x.unwrap(), "air"); // fails }
let x: Option<&str> = None; assert_eq!(x.unwrap(), "air"); // fails
fn expect(self, msg: &str) -> TUnwraps an option, yielding the content of a Some.
Panics if the value is a None with a custom panic message provided by
msg.
let x = Some("value"); assert_eq!(x.expect("the world is ending"), "value");fn main() { let x: Option<&str> = None; x.expect("the world is ending"); // panics with `the world is ending` }
let x: Option<&str> = None; x.expect("the world is ending"); // panics with `the world is ending`
fn unwrap_or(self, def: T) -> TReturns the contained value or a default.
assert_eq!(Some("car").unwrap_or("bike"), "car"); assert_eq!(None.unwrap_or("bike"), "bike");
fn unwrap_or_default(self) -> TReturns the contained value or a default
Consumes the self argument then, if Some, returns the contained
value, otherwise if None, returns the default value for that
type.
Convert a string to an integer, turning poorly-formed strings
into 0 (the default value for integers). parse converts
a string to any other type that implements FromStr, returning
None on error.
let good_year_from_input = "1909"; let bad_year_from_input = "190blarg"; let good_year = good_year_from_input.parse().ok().unwrap_or_default(); let bad_year = bad_year_from_input.parse().ok().unwrap_or_default(); assert_eq!(1909, good_year); assert_eq!(0, bad_year);
fn unwrap_or_else<F>(self, f: F) -> T where F: FnOnce() -> TReturns the contained value or computes it from a closure.
let k = 10; assert_eq!(Some(4).unwrap_or_else(|| 2 * k), 4); assert_eq!(None.unwrap_or_else(|| 2 * k), 20);
fn take(&mut self) -> Option<T>Takes the value out of the option, leaving a None in its place.
let mut x = Some(2); x.take(); assert_eq!(x, None); let mut x: Option<u32> = None; x.take(); assert_eq!(x, None);
fn map<U, F>(self, f: F) -> Option<U> where F: FnOnce(T) -> UMaps an Option<T> to Option<U> by applying a function to a contained value
Convert an Option<String> into an Option<usize>, consuming the original:
let maybe_some_string = Some(String::from("Hello, World!")); // `Option::map` takes self *by value*, consuming `maybe_some_string` let maybe_some_len = maybe_some_string.map(|s| s.len()); assert_eq!(maybe_some_len, Some(13));
fn map_or<U, F>(self, default: U, f: F) -> U where F: FnOnce(T) -> UApplies a function to the contained value (if any),
or returns a default (if not).
let x = Some("foo"); assert_eq!(x.map_or(42, |v| v.len()), 3); let x: Option<&str> = None; assert_eq!(x.map_or(42, |v| v.len()), 42);
fn map_or_else<U, D, F>(self, default: D, f: F) -> U where D: FnOnce() -> U, F: FnOnce(T) -> UApplies a function to the contained value (if any),
or computes a default (if not).
let k = 21; let x = Some("foo"); assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3); let x: Option<&str> = None; assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
fn ok_or<E>(self, err: E) -> Result<T, E>Transforms the Option<T> into a Result<T, E>, mapping Some(v) to
Ok(v) and None to Err(err).
let x = Some("foo"); assert_eq!(x.ok_or(0), Ok("foo")); let x: Option<&str> = None; assert_eq!(x.ok_or(0), Err(0));
fn ok_or_else<E, F>(self, err: F) -> Result<T, E> where F: FnOnce() -> ETransforms the Option<T> into a Result<T, E>, mapping Some(v) to
Ok(v) and None to Err(err()).
let x = Some("foo"); assert_eq!(x.ok_or_else(|| 0), Ok("foo")); let x: Option<&str> = None; assert_eq!(x.ok_or_else(|| 0), Err(0));
fn and<U>(self, optb: Option<U>) -> Option<U>Returns None if the option is None, otherwise returns optb.
let x = Some(2); let y: Option<&str> = None; assert_eq!(x.and(y), None); let x: Option<u32> = None; let y = Some("foo"); assert_eq!(x.and(y), None); let x = Some(2); let y = Some("foo"); assert_eq!(x.and(y), Some("foo")); let x: Option<u32> = None; let y: Option<&str> = None; assert_eq!(x.and(y), None);
fn and_then<U, F>(self, f: F) -> Option<U> where F: FnOnce(T) -> Option<U>Returns None if the option is None, otherwise calls f with the
wrapped value and returns the result.
Some languages call this operation flatmap.
fn sq(x: u32) -> Option<u32> { Some(x * x) } fn nope(_: u32) -> Option<u32> { None } assert_eq!(Some(2).and_then(sq).and_then(sq), Some(16)); assert_eq!(Some(2).and_then(sq).and_then(nope), None); assert_eq!(Some(2).and_then(nope).and_then(sq), None); assert_eq!(None.and_then(sq).and_then(sq), None);
fn or(self, optb: Option<T>) -> Option<T>Returns the option if it contains a value, otherwise returns optb.
let x = Some(2); let y = None; assert_eq!(x.or(y), Some(2)); let x = None; let y = Some(100); assert_eq!(x.or(y), Some(100)); let x = Some(2); let y = Some(100); assert_eq!(x.or(y), Some(2)); let x: Option<u32> = None; let y = None; assert_eq!(x.or(y), None);
fn or_else<F>(self, f: F) -> Option<T> where F: FnOnce() -> Option<T>Returns the option if it contains a value, otherwise calls f and
returns the result.
fn nobody() -> Option<&'static str> { None } fn vikings() -> Option<&'static str> { Some("vikings") } assert_eq!(Some("barbarians").or_else(vikings), Some("barbarians")); assert_eq!(None.or_else(vikings), Some("vikings")); assert_eq!(None.or_else(nobody), None);
impl<T> PartialOrd<Option<T>> for Option<T> where T: PartialOrd<T>fn partial_cmp(&self, __arg_0: &Option<T>) -> Option<Ordering>This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, __arg_0: &Option<T>) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, __arg_0: &Option<T>) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, __arg_0: &Option<T>) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, __arg_0: &Option<T>) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl<T> IntoIterator for Option<T>type Item = TThe type of the elements being iterated over.
type IntoIter = IntoIter<T>Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<T>Returns a consuming iterator over the possibly contained value.
let x = Some("string"); let v: Vec<&str> = x.into_iter().collect(); assert_eq!(v, ["string"]); let x = None; let v: Vec<&str> = x.into_iter().collect(); assert!(v.is_empty());
impl<'a, T> IntoIterator for &'a Option<T>type Item = &'a TThe type of the elements being iterated over.
type IntoIter = Iter<'a, T>Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, T>Creates an iterator from a value. Read more
impl<'a, T> IntoIterator for &'a mut Option<T>impl<A, V> FromIterator<Option<A>> for Option<V> where V: FromIterator<A>fn from_iter<I>(iter: I) -> Option<V> where I: IntoIterator<Item=Option<A>>Takes each element in the Iterator: if it is None, no further
elements are taken, and the None is returned. Should no None occur, a
container with the values of each Option is returned.
Here is an example which increments every integer in a vector, checking for overflow:
fn main() { use std::u16; let v = vec!(1, 2); let res: Option<Vec<u16>> = v.iter().map(|&x: &u16| if x == u16::MAX { None } else { Some(x + 1) } ).collect(); assert!(res == Some(vec!(2, 3))); }use std::u16; let v = vec!(1, 2); let res: Option<Vec<u16>> = v.iter().map(|&x: &u16| if x == u16::MAX { None } else { Some(x + 1) } ).collect(); assert!(res == Some(vec!(2, 3)));
pub trait Hash {
fn hash<H>(&self, state: &mut H) where H: Hasher;
fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher { ... }
}A hashable type.
The H type parameter is an abstract hash state that is used by the Hash
to compute the hash.
If you are also implementing Eq, there is an additional property that
is important:
k1 == k2 -> hash(k1) == hash(k2)
In other words, if two keys are equal, their hashes should also be equal.
HashMap and HashSet both rely on this behavior.
This trait can be used with #[derive] if all fields implement Hash.
When derived, the resulting hash will be the combination of the values
from calling .hash() on each field.
Hash?If you need more control over how a value is hashed, you need to implement
the Hash trait:
use std::hash::{Hash, Hasher}; struct Person { id: u32, name: String, phone: u64, } impl Hash for Person { fn hash<H: Hasher>(&self, state: &mut H) { self.id.hash(state); self.phone.hash(state); } }
fn hash<H>(&self, state: &mut H) where H: HasherFeeds this value into the state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: HasherFeeds a slice of this type into the state provided.
impl<T> Hash for Wrapping<T> where T: Hashimpl<T> Hash for NonZero<T> where T: Zeroable + Hashimpl<Ret> Hash for fn() -> Retimpl<Ret> Hash for extern fn() -> Retimpl<Ret> Hash for unsafe fn() -> Retimpl<Ret> Hash for unsafe extern fn() -> Retimpl<Ret, A> Hash for fn(A) -> Retimpl<Ret, A> Hash for extern fn(A) -> Retimpl<Ret, A> Hash for unsafe fn(A) -> Retimpl<Ret, A> Hash for unsafe extern fn(A) -> Retimpl<Ret, A, B> Hash for fn(A, B) -> Retimpl<Ret, A, B> Hash for extern fn(A, B) -> Retimpl<Ret, A, B> Hash for unsafe fn(A, B) -> Retimpl<Ret, A, B> Hash for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> Hash for fn(A, B, C) -> Retimpl<Ret, A, B, C> Hash for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> Hash for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> Hash for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> Hash for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Hash for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Hash for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Hash for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> Hash for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Hash for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Hash for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Hash for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> Hash for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Hash for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Hash for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Hash for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> Hash for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Hash for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Hash for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Hash for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Hash for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Hash for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Hash for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Hash for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Hash for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Hash for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Hash for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Hash for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Hash for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Hash for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Hash for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<T> Hash for PhantomData<T> where T: ?Sizedimpl Hash for RangeFullimpl<Idx> Hash for Range<Idx> where Idx: Hashimpl<Idx> Hash for RangeFrom<Idx> where Idx: Hashimpl<Idx> Hash for RangeTo<Idx> where Idx: Hashimpl<Idx> Hash for RangeInclusive<Idx> where Idx: Hashimpl<Idx> Hash for RangeToInclusive<Idx> where Idx: Hashimpl Hash for Orderingimpl Hash for TypeIdimpl<T> Hash for [T; 0] where T: Hashimpl<T> Hash for [T; 1] where T: Hashimpl<T> Hash for [T; 2] where T: Hashimpl<T> Hash for [T; 3] where T: Hashimpl<T> Hash for [T; 4] where T: Hashimpl<T> Hash for [T; 5] where T: Hashimpl<T> Hash for [T; 6] where T: Hashimpl<T> Hash for [T; 7] where T: Hashimpl<T> Hash for [T; 8] where T: Hashimpl<T> Hash for [T; 9] where T: Hashimpl<T> Hash for [T; 10] where T: Hashimpl<T> Hash for [T; 11] where T: Hashimpl<T> Hash for [T; 12] where T: Hashimpl<T> Hash for [T; 13] where T: Hashimpl<T> Hash for [T; 14] where T: Hashimpl<T> Hash for [T; 15] where T: Hashimpl<T> Hash for [T; 16] where T: Hashimpl<T> Hash for [T; 17] where T: Hashimpl<T> Hash for [T; 18] where T: Hashimpl<T> Hash for [T; 19] where T: Hashimpl<T> Hash for [T; 20] where T: Hashimpl<T> Hash for [T; 21] where T: Hashimpl<T> Hash for [T; 22] where T: Hashimpl<T> Hash for [T; 23] where T: Hashimpl<T> Hash for [T; 24] where T: Hashimpl<T> Hash for [T; 25] where T: Hashimpl<T> Hash for [T; 26] where T: Hashimpl<T> Hash for [T; 27] where T: Hashimpl<T> Hash for [T; 28] where T: Hashimpl<T> Hash for [T; 29] where T: Hashimpl<T> Hash for [T; 30] where T: Hashimpl<T> Hash for [T; 31] where T: Hashimpl<T> Hash for [T; 32] where T: Hashimpl<T> Hash for Option<T> where T: Hashimpl<T, E> Hash for Result<T, E> where E: Hash, T: Hashimpl Hash for u8impl Hash for u16impl Hash for u32impl Hash for u64impl Hash for usizeimpl Hash for i8impl Hash for i16impl Hash for i32impl Hash for i64impl Hash for isizeimpl Hash for boolimpl Hash for charimpl Hash for strimpl Hash for ()impl<A> Hash for (A,) where A: Hashimpl<A, B> Hash for (A, B) where A: Hash, B: Hashimpl<A, B, C> Hash for (A, B, C) where A: Hash, B: Hash, C: Hashimpl<A, B, C, D> Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hashimpl<A, B, C, D, E> Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hashimpl<A, B, C, D, E, F> Hash for (A, B, C, D, E, F) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hashimpl<A, B, C, D, E, F, G> Hash for (A, B, C, D, E, F, G) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hash, G: Hashimpl<A, B, C, D, E, F, G, H> Hash for (A, B, C, D, E, F, G, H) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hash, G: Hash, H: Hashimpl<A, B, C, D, E, F, G, H, I> Hash for (A, B, C, D, E, F, G, H, I) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hash, G: Hash, H: Hash, I: Hashimpl<A, B, C, D, E, F, G, H, I, J> Hash for (A, B, C, D, E, F, G, H, I, J) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hash, G: Hash, H: Hash, I: Hash, J: Hashimpl<A, B, C, D, E, F, G, H, I, J, K> Hash for (A, B, C, D, E, F, G, H, I, J, K) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hash, G: Hash, H: Hash, I: Hash, J: Hash, K: Hashimpl<A, B, C, D, E, F, G, H, I, J, K, L> Hash for (A, B, C, D, E, F, G, H, I, J, K, L) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash, F: Hash, G: Hash, H: Hash, I: Hash, J: Hash, K: Hash, L: Hashimpl<T> Hash for [T] where T: Hashimpl<'a, T> Hash for &'a T where T: Hash + ?Sizedimpl<'a, T> Hash for &'a mut T where T: Hash + ?Sizedimpl<T> Hash for *const Timpl<T> Hash for *mut Timpl Hash for Errorimpl<T> Hash for Box<T> where T: Hash + ?Sizedimpl<T> Hash for Arc<T> where T: Hash + ?Sizedimpl<T> Hash for Rc<T> where T: Hash + ?Sizedimpl<K, V> Hash for BTreeMap<K, V> where K: Hash, V: Hashimpl<T> Hash for BTreeSet<T> where T: Hashimpl<'a, B> Hash for Cow<'a, B> where B: Hash + ToOwned + ?Sizedimpl<E> Hash for EnumSet<E> where E: Hashimpl<A> Hash for LinkedList<A> where A: Hashimpl Hash for Stringimpl<T> Hash for Vec<T> where T: Hashimpl<A> Hash for VecDeque<A> where A: Hashimpl<T> Hash for Bound<T> where T: Hashimpl Hash for CStringimpl Hash for CStrimpl Hash for OsStringimpl Hash for OsStrimpl Hash for FileTypeimpl Hash for IpAddrimpl Hash for Ipv6MulticastScopeimpl Hash for Ipv4Addrimpl Hash for Ipv6Addrimpl Hash for SocketAddrimpl Hash for SocketAddrV4impl Hash for SocketAddrV6impl<'a> Hash for Prefix<'a>impl<'a> Hash for PrefixComponent<'a>impl<'a> Hash for Component<'a>impl Hash for PathBufimpl Hash for Pathimpl Hash for Durationpub trait Debug {
fn fmt(&self, &mut Formatter) -> Result<(), Error>;
}Format trait for the ? character.
Debug should format the output in a programmer-facing, debugging context.
Generally speaking, you should just derive a Debug implementation.
When used with the alternate format specifier #?, the output is pretty-printed.
For more information on formatters, see the module-level documentation.
This trait can be used with #[derive] if all fields implement Debug. When
derived for structs, it will use the name of the struct, then {, then a
comma-separated list of each field's name and Debug value, then }. For
enums, it will use the name of the variant and, if applicable, (, then the
Debug values of the fields, then ).
Deriving an implementation:
fn main() { #[derive(Debug)] struct Point { x: i32, y: i32, } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:?}", origin); }#[derive(Debug)] struct Point { x: i32, y: i32, } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:?}", origin);
Manually implementing:
fn main() { use std::fmt; struct Point { x: i32, y: i32, } impl fmt::Debug for Point { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y) } } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:?}", origin); }use std::fmt; struct Point { x: i32, y: i32, } impl fmt::Debug for Point { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y) } } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:?}", origin);
This outputs:
The origin is: Point { x: 0, y: 0 }
There are a number of debug_* methods on Formatter to help you with manual
implementations, such as debug_struct.
Debug implementations using either derive or the debug builder API
on Formatter support pretty printing using the alternate flag: {:#?}.
Pretty printing with #?:
#[derive(Debug)] struct Point { x: i32, y: i32, } let origin = Point { x: 0, y: 0 }; println!("The origin is: {:#?}", origin);
This outputs:
The origin is: Point {
x: 0,
y: 0
}
impl<T> Debug for Wrapping<T> where T: Debugimpl Debug for ParseFloatErrorimpl Debug for FpCategoryimpl Debug for TryFromIntErrorimpl Debug for ParseIntErrorimpl<T> Debug for NonZero<T> where T: Zeroable + Debugimpl<Ret> Debug for fn() -> Retimpl<Ret> Debug for extern fn() -> Retimpl<Ret> Debug for unsafe fn() -> Retimpl<Ret> Debug for unsafe extern fn() -> Retimpl<Ret, A> Debug for fn(A) -> Retimpl<Ret, A> Debug for extern fn(A) -> Retimpl<Ret, A> Debug for unsafe fn(A) -> Retimpl<Ret, A> Debug for unsafe extern fn(A) -> Retimpl<Ret, A, B> Debug for fn(A, B) -> Retimpl<Ret, A, B> Debug for extern fn(A, B) -> Retimpl<Ret, A, B> Debug for unsafe fn(A, B) -> Retimpl<Ret, A, B> Debug for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> Debug for fn(A, B, C) -> Retimpl<Ret, A, B, C> Debug for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> Debug for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> Debug for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> Debug for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Debug for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Debug for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Debug for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> Debug for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Debug for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Debug for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Debug for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> Debug for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Debug for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Debug for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Debug for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> Debug for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Debug for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Debug for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Debug for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Debug for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Debug for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Debug for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Debug for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Debug for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Debug for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Debug for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Debug for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Debug for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl Debug for RangeFullimpl<Idx> Debug for Range<Idx> where Idx: Debugimpl<Idx> Debug for RangeFrom<Idx> where Idx: Debugimpl<Idx> Debug for RangeTo<Idx> where Idx: Debugimpl<Idx> Debug for RangeInclusive<Idx> where Idx: Debugimpl<Idx> Debug for RangeToInclusive<Idx> where Idx: Debugimpl Debug for Orderingimpl Debug for Any + 'staticimpl Debug for Any + 'static + Sendimpl Debug for TypeIdimpl<T> Debug for [T; 0] where T: Debugimpl<T> Debug for [T; 1] where T: Debugimpl<T> Debug for [T; 2] where T: Debugimpl<T> Debug for [T; 3] where T: Debugimpl<T> Debug for [T; 4] where T: Debugimpl<T> Debug for [T; 5] where T: Debugimpl<T> Debug for [T; 6] where T: Debugimpl<T> Debug for [T; 7] where T: Debugimpl<T> Debug for [T; 8] where T: Debugimpl<T> Debug for [T; 9] where T: Debugimpl<T> Debug for [T; 10] where T: Debugimpl<T> Debug for [T; 11] where T: Debugimpl<T> Debug for [T; 12] where T: Debugimpl<T> Debug for [T; 13] where T: Debugimpl<T> Debug for [T; 14] where T: Debugimpl<T> Debug for [T; 15] where T: Debugimpl<T> Debug for [T; 16] where T: Debugimpl<T> Debug for [T; 17] where T: Debugimpl<T> Debug for [T; 18] where T: Debugimpl<T> Debug for [T; 19] where T: Debugimpl<T> Debug for [T; 20] where T: Debugimpl<T> Debug for [T; 21] where T: Debugimpl<T> Debug for [T; 22] where T: Debugimpl<T> Debug for [T; 23] where T: Debugimpl<T> Debug for [T; 24] where T: Debugimpl<T> Debug for [T; 25] where T: Debugimpl<T> Debug for [T; 26] where T: Debugimpl<T> Debug for [T; 27] where T: Debugimpl<T> Debug for [T; 28] where T: Debugimpl<T> Debug for [T; 29] where T: Debugimpl<T> Debug for [T; 30] where T: Debugimpl<T> Debug for [T; 31] where T: Debugimpl<T> Debug for [T; 32] where T: Debugimpl Debug for Orderingimpl Debug for AtomicI8impl Debug for AtomicU8impl Debug for AtomicI16impl Debug for AtomicU16impl Debug for AtomicI32impl Debug for AtomicU32impl Debug for AtomicI64impl Debug for AtomicU64impl Debug for AtomicIsizeimpl Debug for AtomicUsizeimpl Debug for AtomicBoolimpl<T> Debug for AtomicPtr<T>impl Debug for BorrowStateimpl Debug for EscapeUnicodeimpl Debug for EscapeDefaultimpl Debug for EncodeUtf8impl Debug for EncodeUtf16impl<A, R> Debug for StepBy<A, R> where A: Debug, R: Debugimpl<A> Debug for Repeat<A> where A: Debugimpl<T> Debug for Empty<T>impl<T> Debug for Once<T> where T: Debugimpl<T> Debug for Rev<T> where T: Debugimpl<I> Debug for Cloned<I> where I: Debugimpl<I> Debug for Cycle<I> where I: Debugimpl<A, B> Debug for Chain<A, B> where A: Debug, B: Debugimpl<A, B> Debug for Zip<A, B> where A: Debug, B: Debugimpl<I, F> Debug for Map<I, F> where I: Debugimpl<I, P> Debug for Filter<I, P> where I: Debugimpl<I, F> Debug for FilterMap<I, F> where I: Debugimpl<I> Debug for Enumerate<I> where I: Debugimpl<I> Debug for Peekable<I> where I: Iterator + Debug, I::Item: Debugimpl<I, P> Debug for SkipWhile<I, P> where I: Debugimpl<I, P> Debug for TakeWhile<I, P> where I: Debugimpl<I> Debug for Skip<I> where I: Debugimpl<I> Debug for Take<I> where I: Debugimpl<I, St, F> Debug for Scan<I, St, F> where I: Debug, St: Debugimpl<I, U, F> Debug for FlatMap<I, U, F> where I: Debug, U: IntoIterator, U::IntoIter: Debugimpl<I> Debug for Fuse<I> where I: Debugimpl<I, F> Debug for Inspect<I, F> where I: Debugimpl<T> Debug for Option<T> where T: Debugimpl<'a, A> Debug for Iter<'a, A> where A: 'a + Debugimpl<'a, A> Debug for IterMut<'a, A> where A: 'a + Debugimpl<A> Debug for IntoIter<A> where A: Debugimpl<T, E> Debug for Result<T, E> where E: Debug, T: Debugimpl<'a, T> Debug for Iter<'a, T> where T: 'a + Debugimpl<'a, T> Debug for IterMut<'a, T> where T: 'a + Debugimpl<T> Debug for IntoIter<T> where T: Debugimpl<'a, T> Debug for Iter<'a, T> where T: 'a + Debugimpl<'a, T> Debug for IterMut<'a, T> where T: 'a + Debugimpl<'a, T, P> Debug for Split<'a, T, P> where P: FnMut(&T) -> bool, T: 'a + Debugimpl<'a, T, P> Debug for SplitMut<'a, T, P> where P: FnMut(&T) -> bool, T: 'a + Debugimpl<'a, T, P> Debug for SplitN<'a, T, P> where P: FnMut(&T) -> bool, T: 'a + Debugimpl<'a, T, P> Debug for RSplitN<'a, T, P> where P: FnMut(&T) -> bool, T: 'a + Debugimpl<'a, T, P> Debug for SplitNMut<'a, T, P> where P: FnMut(&T) -> bool, T: 'a + Debugimpl<'a, T, P> Debug for RSplitNMut<'a, T, P> where P: FnMut(&T) -> bool, T: 'a + Debugimpl<'a, T> Debug for Windows<'a, T> where T: 'a + Debugimpl<'a, T> Debug for Chunks<'a, T> where T: 'a + Debugimpl<'a, T> Debug for ChunksMut<'a, T> where T: 'a + Debugimpl Debug for SearchStepimpl<'a> Debug for CharSearcher<'a>impl<'a, 'b> Debug for CharSliceSearcher<'a, 'b>impl<'a, F> Debug for CharPredicateSearcher<'a, F> where F: FnMut(char) -> boolimpl<'a, 'b> Debug for StrSearcher<'a, 'b>impl Debug for ParseBoolErrorimpl Debug for Utf8Errorimpl<'a> Debug for Chars<'a>impl<'a> Debug for CharIndices<'a>impl<'a> Debug for Bytes<'a>impl<'a, P> Debug for Split<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for RSplit<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for SplitTerminator<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for RSplitTerminator<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for SplitN<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for RSplitN<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for MatchIndices<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for RMatchIndices<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for Matches<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a, P> Debug for RMatches<'a, P> where P: Pattern<'a>, P::Searcher: Debugimpl<'a> Debug for Lines<'a>impl<'a> Debug for LinesAny<'a>impl Debug for SipHasher13impl Debug for SipHasher24impl Debug for SipHasherimpl<H> Debug for BuildHasherDefault<H>impl Debug for Alignmentimpl Debug for isizeimpl Debug for usizeimpl Debug for i8impl Debug for u8impl Debug for i16impl Debug for u16impl Debug for i32impl Debug for u32impl Debug for i64impl Debug for u64impl Debug for Errorimpl<'a> Debug for Arguments<'a>impl<'a, T> Debug for &'a T where T: Debug + ?Sizedimpl<'a, T> Debug for &'a mut T where T: Debug + ?Sizedimpl Debug for boolimpl Debug for strimpl Debug for charimpl Debug for f32impl Debug for f64impl<T> Debug for *const Timpl<T> Debug for *mut Timpl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where T0: Debug, T1: Debug, T10: Debug, T11: Debug, T2: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where T1: Debug, T10: Debug, T11: Debug, T2: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where T10: Debug, T11: Debug, T2: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T3, T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T3, T4, T5, T6, T7, T8, T9, T10, T11) where T10: Debug, T11: Debug, T3: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T4, T5, T6, T7, T8, T9, T10, T11> Debug for (T4, T5, T6, T7, T8, T9, T10, T11) where T10: Debug, T11: Debug, T4: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T5, T6, T7, T8, T9, T10, T11> Debug for (T5, T6, T7, T8, T9, T10, T11) where T10: Debug, T11: Debug, T5: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T6, T7, T8, T9, T10, T11> Debug for (T6, T7, T8, T9, T10, T11) where T10: Debug, T11: Debug, T6: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T7, T8, T9, T10, T11> Debug for (T7, T8, T9, T10, T11) where T10: Debug, T11: Debug, T7: Debug, T8: Debug, T9: Debugimpl<T8, T9, T10, T11> Debug for (T8, T9, T10, T11) where T10: Debug, T11: Debug, T8: Debug, T9: Debugimpl<T9, T10, T11> Debug for (T9, T10, T11) where T10: Debug, T11: Debug, T9: Debugimpl<T10, T11> Debug for (T10, T11) where T10: Debug, T11: Debugimpl<T11> Debug for (T11,) where T11: Debugimpl<T> Debug for [T] where T: Debugimpl Debug for ()impl<T> Debug for PhantomData<T> where T: ?Sizedimpl<T> Debug for Cell<T> where T: Copy + Debugimpl<T> Debug for RefCell<T> where T: Debug + ?Sizedimpl<'b, T> Debug for Ref<'b, T> where T: Debug + ?Sizedimpl<'b, T> Debug for RefMut<'b, T> where T: Debug + ?Sizedimpl<T> Debug for UnsafeCell<T> where T: Debug + ?Sizedimpl<T> Debug for Box<T> where T: Debug + ?Sizedimpl<T> Debug for Weak<T> where T: Debug + ?Sizedimpl<T> Debug for Arc<T> where T: Debug + ?Sizedimpl<T> Debug for Rc<T> where T: Debug + ?Sizedimpl<T> Debug for Weak<T> where T: Debug + ?Sizedimpl<T> Debug for BinaryHeap<T> where T: Ord + Debugimpl<K, V> Debug for BTreeMap<K, V> where K: Debug, V: Debugimpl<T> Debug for BTreeSet<T> where T: Debugimpl<'a, B> Debug for Cow<'a, B> where B: Debug + ToOwned + ?Sized, B::Owned: Debugimpl<E> Debug for EnumSet<E> where E: CLike + Debugimpl<A> Debug for LinkedList<A> where A: Debugimpl Debug for FromUtf8Errorimpl Debug for FromUtf16Errorimpl Debug for Stringimpl Debug for ParseErrorimpl<T> Debug for Vec<T> where T: Debugimpl<T> Debug for VecDeque<T> where T: Debugimpl<T> Debug for Bound<T> where T: Debugimpl Debug for DecodeUtf16Errorimpl Debug for Threadimpl<K, V, S> Debug for HashMap<K, V, S> where K: Eq + Hash + Debug, V: Debug, S: BuildHasherimpl<T, S> Debug for HashSet<T, S> where T: Eq + Hash + Debug, S: BuildHasherimpl Debug for VarErrorimpl Debug for JoinPathsErrorimpl Debug for NulErrorimpl Debug for FromBytesWithNulErrorimpl Debug for IntoStringErrorimpl Debug for CStringimpl Debug for CStrimpl Debug for OsStringimpl Debug for OsStrimpl Debug for Permissionsimpl Debug for Fileimpl<R> Debug for BufReader<R> where R: Debugimpl<W: Debug> Debug for IntoInnerError<W>impl<W: Write> Debug for BufWriter<W> where W: Debugimpl<W: Write> Debug for LineWriter<W> where W: Debugimpl<T: Debug> Debug for Cursor<T>impl Debug for Errorimpl Debug for ErrorKindimpl Debug for SeekFromimpl Debug for CharsErrorimpl Debug for IpAddrimpl Debug for Ipv6MulticastScopeimpl Debug for Ipv4Addrimpl Debug for Ipv6Addrimpl Debug for SocketAddrimpl Debug for SocketAddrV4impl Debug for SocketAddrV6impl Debug for TcpStreamimpl Debug for TcpListenerimpl Debug for UdpSocketimpl Debug for AddrParseErrorimpl Debug for Shutdownimpl Debug for SocketAddrimpl Debug for UnixStreamimpl Debug for UnixListenerimpl<'a> Debug for Incoming<'a>impl Debug for UnixDatagramimpl<'a> Debug for Prefix<'a>impl<'a> Debug for PrefixComponent<'a>impl<'a> Debug for Component<'a>impl Debug for PathBufimpl Debug for StripPrefixErrorimpl Debug for Pathimpl<'a> Debug for Display<'a>impl Debug for Commandimpl Debug for Outputimpl Debug for ExitStatusimpl Debug for Selectimpl<'rx, T: Send + 'rx> Debug for Handle<'rx, T>impl Debug for RecvErrorimpl Debug for TryRecvErrorimpl Debug for RecvTimeoutErrorimpl<T> Debug for Sender<T>impl<T> Debug for SyncSender<T>impl<T> Debug for Receiver<T>impl<T> Debug for SendError<T>impl<T> Debug for TrySendError<T>impl Debug for WaitTimeoutResultimpl<T: ?Sized + Debug> Debug for Mutex<T>impl<T: ?Sized + Debug> Debug for RwLock<T>impl Debug for Durationimpl Debug for SystemTimeErrorimpl Debug for Instantimpl Debug for SystemTimeimpl<T> Debug for PoisonError<T>impl<T> Debug for TryLockError<T>pub trait Copy: Clone { }
Types that can be copied by simply copying bits (i.e. memcpy).
By default, variable bindings have 'move semantics.' In other words:
fn main() { #[derive(Debug)] struct Foo; let x = Foo; let y = x; // `x` has moved into `y`, and so cannot be used // println!("{:?}", x); // error: use of moved value }#[derive(Debug)] struct Foo; let x = Foo; let y = x; // `x` has moved into `y`, and so cannot be used // println!("{:?}", x); // error: use of moved value
However, if a type implements Copy, it instead has 'copy semantics':
// we can just derive a `Copy` implementation #[derive(Debug, Copy, Clone)] struct Foo; let x = Foo; let y = x; // `y` is a copy of `x` println!("{:?}", x); // A-OK!
It's important to note that in these two examples, the only difference is if you are allowed to
access x after the assignment: a move is also a bitwise copy under the hood.
Copy?A type can implement Copy if all of its components implement Copy. For example, this
struct can be Copy:
struct Point { x: i32, y: i32, }
A struct can be Copy, and i32 is Copy, so therefore, Point is eligible to be Copy.
struct PointList { points: Vec<Point>, }
The PointList struct cannot implement Copy, because Vec<T> is not Copy. If we
attempt to derive a Copy implementation, we'll get an error:
the trait `Copy` may not be implemented for this type; field `points` does not implement `Copy`
Copy?Some types can't be copied safely. For example, copying &mut T would create an aliased
mutable reference, and copying String would result in two attempts to free the same buffer.
Generalizing the latter case, any type implementing Drop can't be Copy, because it's
managing some resource besides its own size_of::<T>() bytes.
Copy?Generally speaking, if your type can implement Copy, it should. There's one important thing
to consider though: if you think your type may not be able to implement Copy in the future,
then it might be prudent to not implement Copy. This is because removing Copy is a breaking
change: that second example would fail to compile if we made Foo non-Copy.
This trait can be used with #[derive] if all of its components implement Copy and the type
implements Clone. The implementation will copy the bytes of each field using memcpy.
Copy?There are two ways to implement Copy on your type:
#[derive(Copy, Clone)] struct MyStruct;
and
fn main() { struct MyStruct; impl Copy for MyStruct {} impl Clone for MyStruct { fn clone(&self) -> MyStruct { *self } } }struct MyStruct; impl Copy for MyStruct {} impl Clone for MyStruct { fn clone(&self) -> MyStruct { *self } }
There is a small difference between the two: the derive strategy will also place a Copy
bound on type parameters, which isn't always desired.
impl<T> Copy for Wrapping<T> where T: Copyimpl Copy for FpCategoryimpl Copy for TryFromIntErrorimpl<T> Copy for NonZero<T> where T: Copy + Zeroableimpl<T> Copy for Shared<T> where T: ?Sizedimpl<T> Copy for PhantomData<T> where T: ?Sizedimpl Copy for RangeFullimpl<Idx> Copy for RangeTo<Idx> where Idx: Copyimpl<Idx> Copy for RangeToInclusive<Idx> where Idx: Copyimpl Copy for Orderingimpl Copy for TypeIdimpl Copy for Orderingimpl Copy for BorrowStateimpl<T> Copy for Option<T> where T: Copyimpl Copy for TraitObjectimpl<T, E> Copy for Result<T, E> where E: Copy, T: Copyimpl Copy for SearchStepimpl Copy for Utf8Errorimpl Copy for Errorimpl<'a> Copy for Arguments<'a>impl Copy for ExchangeHeapSingletonimpl<E> Copy for EnumSet<E>impl Copy for ParseErrorimpl<T> Copy for Bound<T> where T: Copyimpl Copy for LocalKeyStateimpl Copy for FileTypeimpl Copy for ErrorKindimpl Copy for SeekFromimpl Copy for IpAddrimpl Copy for Ipv4Addrimpl Copy for Ipv6Addrimpl Copy for Ipv6MulticastScopeimpl Copy for SocketAddrimpl Copy for SocketAddrV4impl Copy for SocketAddrV6impl Copy for Shutdownimpl<'a> Copy for Prefix<'a>impl<'a> Copy for PrefixComponent<'a>impl<'a> Copy for Component<'a>impl Copy for ExitStatusimpl<T: Copy> Copy for SendError<T>impl Copy for RecvErrorimpl Copy for TryRecvErrorimpl Copy for RecvTimeoutErrorimpl<T: Copy> Copy for TrySendError<T>impl Copy for WaitTimeoutResultimpl Copy for Durationimpl Copy for Instantimpl Copy for SystemTimeimpl Copy for StandardNormalimpl Copy for Normalimpl Copy for LogNormalimpl Copy for Exp1impl Copy for Expimpl Copy for IsaacRngimpl Copy for Isaac64Rngimpl Copy for ChaChaRngimpl Copy for ReseedWithDefaultpub trait Clone {
fn clone(&self) -> Self;
fn clone_from(&mut self, source: &Self) { ... }
}A common trait for the ability to explicitly duplicate an object.
Differs from Copy in that Copy is implicit and extremely inexpensive, while
Clone is always explicit and may or may not be expensive. In order to enforce
these characteristics, Rust does not allow you to reimplement Copy, but you
may reimplement Clone and run arbitrary code.
Since Clone is more general than Copy, you can automatically make anything
Copy be Clone as well.
This trait can be used with #[derive] if all fields are Clone. The derived
implementation of clone() calls clone() on each field.
Clone?Types that are Copy should have a trivial implementation of Clone. More formally:
if T: Copy, x: T, and y: &T, then let x = y.clone(); is equivalent to let x = *y;.
Manual implementations should be careful to uphold this invariant; however, unsafe code
must not rely on it to ensure memory safety.
An example is an array holding more than 32 elements of a type that is Clone; the standard
library only implements Clone up until arrays of size 32. In this case, the implementation of
Clone cannot be derived, but can be implemented as:
#[derive(Copy)] struct Stats { frequencies: [i32; 100], } impl Clone for Stats { fn clone(&self) -> Stats { *self } }
fn clone(&self) -> SelfReturns a copy of the value.
let hello = "Hello"; // &str implements Clone assert_eq!("Hello", hello.clone());
fn clone_from(&mut self, source: &Self)Performs copy-assignment from source.
a.clone_from(&b) is equivalent to a = b.clone() in functionality,
but can be overridden to reuse the resources of a to avoid unnecessary
allocations.
impl<T> Clone for Wrapping<T> where T: Cloneimpl Clone for ParseFloatErrorimpl Clone for FpCategoryimpl Clone for TryFromIntErrorimpl Clone for ParseIntErrorimpl<T> Clone for NonZero<T> where T: Zeroable + Cloneimpl<T> Clone for *const T where T: ?Sizedimpl<T> Clone for *mut T where T: ?Sizedimpl<Ret> Clone for fn() -> Retimpl<Ret> Clone for extern fn() -> Retimpl<Ret> Clone for unsafe fn() -> Retimpl<Ret> Clone for unsafe extern fn() -> Retimpl<Ret, A> Clone for fn(A) -> Retimpl<Ret, A> Clone for extern fn(A) -> Retimpl<Ret, A> Clone for unsafe fn(A) -> Retimpl<Ret, A> Clone for unsafe extern fn(A) -> Retimpl<Ret, A, B> Clone for fn(A, B) -> Retimpl<Ret, A, B> Clone for extern fn(A, B) -> Retimpl<Ret, A, B> Clone for unsafe fn(A, B) -> Retimpl<Ret, A, B> Clone for unsafe extern fn(A, B) -> Retimpl<Ret, A, B, C> Clone for fn(A, B, C) -> Retimpl<Ret, A, B, C> Clone for extern fn(A, B, C) -> Retimpl<Ret, A, B, C> Clone for unsafe fn(A, B, C) -> Retimpl<Ret, A, B, C> Clone for unsafe extern fn(A, B, C) -> Retimpl<Ret, A, B, C, D> Clone for fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Clone for extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Clone for unsafe fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D> Clone for unsafe extern fn(A, B, C, D) -> Retimpl<Ret, A, B, C, D, E> Clone for fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Clone for extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Clone for unsafe fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E> Clone for unsafe extern fn(A, B, C, D, E) -> Retimpl<Ret, A, B, C, D, E, F> Clone for fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Clone for extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Clone for unsafe fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F> Clone for unsafe extern fn(A, B, C, D, E, F) -> Retimpl<Ret, A, B, C, D, E, F, G> Clone for fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Clone for extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Clone for unsafe fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G> Clone for unsafe extern fn(A, B, C, D, E, F, G) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Clone for fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Clone for extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Clone for unsafe fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H> Clone for unsafe extern fn(A, B, C, D, E, F, G, H) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Clone for fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Clone for extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Clone for unsafe fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I> Clone for unsafe extern fn(A, B, C, D, E, F, G, H, I) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Clone for fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Clone for extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Clone for unsafe fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J> Clone for unsafe extern fn(A, B, C, D, E, F, G, H, I, J) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Clone for fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Clone for extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Clone for unsafe fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K> Clone for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Clone for fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Clone for extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Clone for unsafe fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<Ret, A, B, C, D, E, F, G, H, I, J, K, L> Clone for unsafe extern fn(A, B, C, D, E, F, G, H, I, J, K, L) -> Retimpl<T> Clone for Shared<T> where T: ?Sizedimpl<T> Clone for PhantomData<T> where T: ?Sizedimpl Clone for RangeFullimpl<Idx> Clone for Range<Idx> where Idx: Cloneimpl<Idx> Clone for RangeFrom<Idx> where Idx: Cloneimpl<Idx> Clone for RangeTo<Idx> where Idx: Cloneimpl<Idx> Clone for RangeInclusive<Idx> where Idx: Cloneimpl<Idx> Clone for RangeToInclusive<Idx> where Idx: Cloneimpl Clone for Orderingimpl<'a, T> Clone for &'a T where T: ?Sizedimpl Clone for isizeimpl Clone for i8impl Clone for i16impl Clone for i32impl Clone for i64impl Clone for usizeimpl Clone for u8impl Clone for u16impl Clone for u32impl Clone for u64impl Clone for f32impl Clone for f64impl Clone for ()impl Clone for boolimpl Clone for charimpl Clone for TypeIdimpl<T> Clone for [T; 0] where T: Copyimpl<T> Clone for [T; 1] where T: Copyimpl<T> Clone for [T; 2] where T: Copyimpl<T> Clone for [T; 3] where T: Copyimpl<T> Clone for [T; 4] where T: Copyimpl<T> Clone for [T; 5] where T: Copyimpl<T> Clone for [T; 6] where T: Copyimpl<T> Clone for [T; 7] where T: Copyimpl<T> Clone for [T; 8] where T: Copyimpl<T> Clone for [T; 9] where T: Copyimpl<T> Clone for [T; 10] where T: Copyimpl<T> Clone for [T; 11] where T: Copyimpl<T> Clone for [T; 12] where T: Copyimpl<T> Clone for [T; 13] where T: Copyimpl<T> Clone for [T; 14] where T: Copyimpl<T> Clone for [T; 15] where T: Copyimpl<T> Clone for [T; 16] where T: Copyimpl<T> Clone for [T; 17] where T: Copyimpl<T> Clone for [T; 18] where T: Copyimpl<T> Clone for [T; 19] where T: Copyimpl<T> Clone for [T; 20] where T: Copyimpl<T> Clone for [T; 21] where T: Copyimpl<T> Clone for [T; 22] where T: Copyimpl<T> Clone for [T; 23] where T: Copyimpl<T> Clone for [T; 24] where T: Copyimpl<T> Clone for [T; 25] where T: Copyimpl<T> Clone for [T; 26] where T: Copyimpl<T> Clone for [T; 27] where T: Copyimpl<T> Clone for [T; 28] where T: Copyimpl<T> Clone for [T; 29] where T: Copyimpl<T> Clone for [T; 30] where T: Copyimpl<T> Clone for [T; 31] where T: Copyimpl<T> Clone for [T; 32] where T: Copyimpl Clone for Orderingimpl<T> Clone for Cell<T> where T: Copyimpl Clone for BorrowStateimpl<T> Clone for RefCell<T> where T: Cloneimpl Clone for EscapeUnicodeimpl Clone for EscapeDefaultimpl<A, R> Clone for StepBy<A, R> where A: Clone, R: Cloneimpl<A> Clone for Repeat<A> where A: Cloneimpl<T> Clone for Empty<T>impl<T> Clone for Once<T> where T: Cloneimpl<T> Clone for Rev<T> where T: Cloneimpl<I> Clone for Cloned<I> where I: Cloneimpl<I> Clone for Cycle<I> where I: Cloneimpl<A, B> Clone for Chain<A, B> where A: Clone, B: Cloneimpl<A, B> Clone for Zip<A, B> where A: Clone, B: Cloneimpl<I, F> Clone for Map<I, F> where F: Clone, I: Cloneimpl<I, P> Clone for Filter<I, P> where I: Clone, P: Cloneimpl<I, F> Clone for FilterMap<I, F> where F: Clone, I: Cloneimpl<I> Clone for Enumerate<I> where I: Cloneimpl<I> Clone for Peekable<I> where I: Clone + Iterator, I::Item: Cloneimpl<I, P> Clone for SkipWhile<I, P> where I: Clone, P: Cloneimpl<I, P> Clone for TakeWhile<I, P> where I: Clone, P: Cloneimpl<I> Clone for Skip<I> where I: Cloneimpl<I> Clone for Take<I> where I: Cloneimpl<I, St, F> Clone for Scan<I, St, F> where F: Clone, I: Clone, St: Cloneimpl<I, U, F> Clone for FlatMap<I, U, F> where F: Clone, I: Clone, U: Clone + IntoIterator, U::IntoIter: Cloneimpl<I> Clone for Fuse<I> where I: Cloneimpl<I, F> Clone for Inspect<I, F> where F: Clone, I: Cloneimpl<T> Clone for Option<T> where T: Cloneimpl<'a, A> Clone for Iter<'a, A>impl<A> Clone for IntoIter<A> where A: Cloneimpl Clone for TraitObjectimpl<T, E> Clone for Result<T, E> where E: Clone, T: Cloneimpl<'a, T> Clone for Iter<'a, T>impl<'a, T> Clone for Iter<'a, T>impl<'a, T, P> Clone for Split<'a, T, P> where P: Clone + FnMut(&T) -> boolimpl<'a, T> Clone for Windows<'a, T>impl<'a, T> Clone for Chunks<'a, T>impl Clone for SearchStepimpl<'a> Clone for CharSearcher<'a>impl<'a, 'b> Clone for CharSliceSearcher<'a, 'b>impl<'a, F> Clone for CharPredicateSearcher<'a, F> where F: Clone + FnMut(char) -> boolimpl<'a, 'b> Clone for StrSearcher<'a, 'b>impl Clone for ParseBoolErrorimpl Clone for Utf8Errorimpl<'a> Clone for Chars<'a>impl<'a> Clone for CharIndices<'a>impl<'a> Clone for Bytes<'a>impl<'a, P> Clone for Split<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for RSplit<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for SplitTerminator<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for RSplitTerminator<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for SplitN<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for RSplitN<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for MatchIndices<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for RMatchIndices<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for Matches<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a, P> Clone for RMatches<'a, P> where P: Pattern<'a>, P::Searcher: Cloneimpl<'a> Clone for Lines<'a>impl<'a> Clone for LinesAny<'a>impl Clone for SipHasher13impl Clone for SipHasher24impl Clone for SipHasherimpl<H> Clone for BuildHasherDefault<H>impl Clone for Errorimpl<'a> Clone for Arguments<'a>impl<A> Clone for (A,) where A: Cloneimpl<A, B> Clone for (A, B) where A: Clone, B: Cloneimpl<A, B, C> Clone for (A, B, C) where A: Clone, B: Clone, C: Cloneimpl<A, B, C, D> Clone for (A, B, C, D) where A: Clone, B: Clone, C: Clone, D: Cloneimpl<A, B, C, D, E> Clone for (A, B, C, D, E) where A: Clone, B: Clone, C: Clone, D: Clone, E: Cloneimpl<A, B, C, D, E, F> Clone for (A, B, C, D, E, F) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Cloneimpl<A, B, C, D, E, F, G> Clone for (A, B, C, D, E, F, G) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Clone, G: Cloneimpl<A, B, C, D, E, F, G, H> Clone for (A, B, C, D, E, F, G, H) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Clone, G: Clone, H: Cloneimpl<A, B, C, D, E, F, G, H, I> Clone for (A, B, C, D, E, F, G, H, I) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Clone, G: Clone, H: Clone, I: Cloneimpl<A, B, C, D, E, F, G, H, I, J> Clone for (A, B, C, D, E, F, G, H, I, J) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Clone, G: Clone, H: Clone, I: Clone, J: Cloneimpl<A, B, C, D, E, F, G, H, I, J, K> Clone for (A, B, C, D, E, F, G, H, I, J, K) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Clone, G: Clone, H: Clone, I: Clone, J: Clone, K: Cloneimpl<A, B, C, D, E, F, G, H, I, J, K, L> Clone for (A, B, C, D, E, F, G, H, I, J, K, L) where A: Clone, B: Clone, C: Clone, D: Clone, E: Clone, F: Clone, G: Clone, H: Clone, I: Clone, J: Clone, K: Clone, L: Cloneimpl Clone for ExchangeHeapSingletonimpl<T> Clone for Box<T> where T: Cloneimpl Clone for Box<str>impl<T> Clone for Box<[T]> where T: Cloneimpl<T> Clone for Arc<T> where T: ?Sizedimpl<T> Clone for Weak<T> where T: ?Sizedimpl<T> Clone for Rc<T> where T: ?Sizedimpl<T> Clone for Weak<T> where T: ?Sizedimpl<T> Clone for BinaryHeap<T> where T: Cloneimpl<'a, T> Clone for Iter<'a, T>impl<T> Clone for IntoIter<T> where T: Cloneimpl<K, V> Clone for BTreeMap<K, V> where K: Clone, V: Cloneimpl<'a, K, V> Clone for Iter<'a, K, V>impl<'a, K, V> Clone for Keys<'a, K, V>impl<'a, K, V> Clone for Values<'a, K, V>impl<'a, K, V> Clone for Range<'a, K, V>impl<T> Clone for BTreeSet<T> where T: Cloneimpl<'a, T> Clone for Iter<'a, T>impl<'a, T> Clone for Range<'a, T>impl<'a, T> Clone for Difference<'a, T>impl<'a, T> Clone for SymmetricDifference<'a, T>impl<'a, T> Clone for Intersection<'a, T>impl<'a, T> Clone for Union<'a, T>impl<'a, B> Clone for Cow<'a, B> where B: ToOwned + ?Sizedimpl<E> Clone for EnumSet<E>impl<E> Clone for Iter<E>impl<'a, T> Clone for Iter<'a, T>impl<T> Clone for IntoIter<T> where T: Cloneimpl<A> Clone for LinkedList<A> where A: Cloneimpl<'a> Clone for EncodeUtf16<'a>impl Clone for Stringimpl Clone for ParseErrorimpl<T> Clone for Vec<T> where T: Cloneimpl<T> Clone for IntoIter<T> where T: Cloneimpl<T> Clone for VecDeque<T> where T: Cloneimpl<'a, T> Clone for Iter<'a, T>impl<T> Clone for IntoIter<T> where T: Cloneimpl<T> Clone for Bound<T> where T: Cloneimpl<I> Clone for Utf16Encoder<I> where I: Cloneimpl<I> Clone for DecodeUtf16<I> where I: Clone + Iterator<Item=u16>impl Clone for DecodeUtf16Errorimpl Clone for LocalKeyStateimpl Clone for Threadimpl<K: Clone, V: Clone, S: Clone> Clone for HashMap<K, V, S>impl<'a, K, V> Clone for Iter<'a, K, V>impl<'a, K, V> Clone for Keys<'a, K, V>impl<'a, K, V> Clone for Values<'a, K, V>impl Clone for RandomStateimpl<T: Clone, S: Clone> Clone for HashSet<T, S>impl<'a, K> Clone for Iter<'a, K>impl<'a, T, S> Clone for Intersection<'a, T, S>impl<'a, T, S> Clone for Difference<'a, T, S>impl<'a, T, S> Clone for SymmetricDifference<'a, T, S>impl<'a, T, S> Clone for Union<'a, T, S>impl Clone for VarErrorimpl Clone for CStringimpl Clone for NulErrorimpl Clone for FromBytesWithNulErrorimpl Clone for IntoStringErrorimpl Clone for OsStringimpl Clone for Metadataimpl Clone for OpenOptionsimpl Clone for Permissionsimpl Clone for FileTypeimpl<T: Clone> Clone for Cursor<T>impl Clone for ErrorKindimpl Clone for SeekFromimpl Clone for IpAddrimpl Clone for Ipv6MulticastScopeimpl Clone for Ipv4Addrimpl Clone for Ipv6Addrimpl Clone for SocketAddrimpl Clone for SocketAddrV4impl Clone for SocketAddrV6impl Clone for AddrParseErrorimpl Clone for Shutdownimpl Clone for SocketAddrimpl Clone for statimpl<'a> Clone for Prefix<'a>impl<'a> Clone for PrefixComponent<'a>impl<'a> Clone for Component<'a>impl<'a> Clone for Components<'a>impl<'a> Clone for Iter<'a>impl Clone for PathBufimpl Clone for StripPrefixErrorimpl Clone for Outputimpl Clone for ExitStatusimpl<T: Clone> Clone for SendError<T>impl Clone for RecvErrorimpl Clone for TryRecvErrorimpl Clone for RecvTimeoutErrorimpl<T: Clone> Clone for TrySendError<T>impl<T> Clone for Sender<T>impl<T> Clone for SyncSender<T>impl Clone for WaitTimeoutResultimpl Clone for Durationimpl Clone for Instantimpl Clone for SystemTimeimpl Clone for SystemTimeErrorimpl Clone for StandardNormalimpl Clone for Normalimpl Clone for LogNormalimpl Clone for Exp1impl Clone for Expimpl Clone for IsaacRngimpl Clone for Isaac64Rngimpl Clone for ChaChaRngimpl Clone for ReseedWithDefaultimpl Clone for XorShiftRngpub trait Default {
fn default() -> Self;
}A trait for giving a type a useful default value.
Sometimes, you want to fall back to some kind of default value, and
don't particularly care what it is. This comes up often with structs
that define a set of options:
struct SomeOptions { foo: i32, bar: f32, }
How can we define some default values? You can use Default:
#[derive(Default)] struct SomeOptions { foo: i32, bar: f32, } fn main() { let options: SomeOptions = Default::default(); }
Now, you get all of the default values. Rust implements Default for various primitives types.
If you want to override a particular option, but still retain the other defaults:
#[allow(dead_code)] #[derive(Default)] struct SomeOptions { foo: i32, bar: f32, } fn main() { let options = SomeOptions { foo: 42, ..Default::default() }; }fn main() { let options = SomeOptions { foo: 42, ..Default::default() }; }
This trait can be used with #[derive] if all of the type's fields implement
Default. When derived, it will use the default value for each field's type.
Default?Provide an implementation for the default() method that returns the value of
your type that should be the default:
enum Kind { A, B, C, } impl Default for Kind { fn default() -> Kind { Kind::A } }
#[derive(Default)] struct SomeOptions { foo: i32, bar: f32, }
fn default() -> SelfReturns the "default value" for a type.
Default values are often some kind of initial value, identity value, or anything else that may make sense as a default.
Using built-in default values:
fn main() { let i: i8 = Default::default(); let (x, y): (Option<String>, f64) = Default::default(); let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default(); }let i: i8 = Default::default(); let (x, y): (Option<String>, f64) = Default::default(); let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
Making your own:
fn main() { #[allow(dead_code)] enum Kind { A, B, C, } impl Default for Kind { fn default() -> Kind { Kind::A } } }enum Kind { A, B, C, } impl Default for Kind { fn default() -> Kind { Kind::A } }
impl<T> Default for Wrapping<T> where T: Defaultimpl<T> Default for PhantomData<T> where T: ?Sizedimpl Default for ()impl Default for boolimpl Default for charimpl Default for usizeimpl Default for u8impl Default for u16impl Default for u32impl Default for u64impl Default for isizeimpl Default for i8impl Default for i16impl Default for i32impl Default for i64impl Default for f32impl Default for f64impl<T> Default for [T; 32] where T: Defaultimpl<T> Default for [T; 31] where T: Defaultimpl<T> Default for [T; 30] where T: Defaultimpl<T> Default for [T; 29] where T: Defaultimpl<T> Default for [T; 28] where T: Defaultimpl<T> Default for [T; 27] where T: Defaultimpl<T> Default for [T; 26] where T: Defaultimpl<T> Default for [T; 25] where T: Defaultimpl<T> Default for [T; 24] where T: Defaultimpl<T> Default for [T; 23] where T: Defaultimpl<T> Default for [T; 22] where T: Defaultimpl<T> Default for [T; 21] where T: Defaultimpl<T> Default for [T; 20] where T: Defaultimpl<T> Default for [T; 19] where T: Defaultimpl<T> Default for [T; 18] where T: Defaultimpl<T> Default for [T; 17] where T: Defaultimpl<T> Default for [T; 16] where T: Defaultimpl<T> Default for [T; 15] where T: Defaultimpl<T> Default for [T; 14] where T: Defaultimpl<T> Default for [T; 13] where T: Defaultimpl<T> Default for [T; 12] where T: Defaultimpl<T> Default for [T; 11] where T: Defaultimpl<T> Default for [T; 10] where T: Defaultimpl<T> Default for [T; 9] where T: Defaultimpl<T> Default for [T; 8] where T: Defaultimpl<T> Default for [T; 7] where T: Defaultimpl<T> Default for [T; 6] where T: Defaultimpl<T> Default for [T; 5] where T: Defaultimpl<T> Default for [T; 4] where T: Defaultimpl<T> Default for [T; 3] where T: Defaultimpl<T> Default for [T; 2] where T: Defaultimpl<T> Default for [T; 1] where T: Defaultimpl<T> Default for [T; 0]impl Default for AtomicBoolimpl<T> Default for AtomicPtr<T>impl Default for AtomicI8impl Default for AtomicU8impl Default for AtomicI16impl Default for AtomicU16impl Default for AtomicI32impl Default for AtomicU32impl Default for AtomicI64impl Default for AtomicU64impl Default for AtomicIsizeimpl Default for AtomicUsizeimpl<T> Default for Cell<T> where T: Copy + Defaultimpl<T> Default for RefCell<T> where T: Defaultimpl<T> Default for UnsafeCell<T> where T: Defaultimpl<T> Default for Empty<T>impl<T> Default for Option<T>impl<'a, T> Default for &'a [T]impl<'a, T> Default for &'a mut [T]impl<'a> Default for &'a strimpl Default for SipHasher13impl Default for SipHasher24impl Default for SipHasherimpl<H> Default for BuildHasherDefault<H>impl Default for Errorimpl<A> Default for (A,) where A: Defaultimpl<A, B> Default for (A, B) where A: Default, B: Defaultimpl<A, B, C> Default for (A, B, C) where A: Default, B: Default, C: Defaultimpl<A, B, C, D> Default for (A, B, C, D) where A: Default, B: Default, C: Default, D: Defaultimpl<A, B, C, D, E> Default for (A, B, C, D, E) where A: Default, B: Default, C: Default, D: Default, E: Defaultimpl<A, B, C, D, E, F> Default for (A, B, C, D, E, F) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Defaultimpl<A, B, C, D, E, F, G> Default for (A, B, C, D, E, F, G) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Default, G: Defaultimpl<A, B, C, D, E, F, G, H> Default for (A, B, C, D, E, F, G, H) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Default, G: Default, H: Defaultimpl<A, B, C, D, E, F, G, H, I> Default for (A, B, C, D, E, F, G, H, I) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Default, G: Default, H: Default, I: Defaultimpl<A, B, C, D, E, F, G, H, I, J> Default for (A, B, C, D, E, F, G, H, I, J) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Default, G: Default, H: Default, I: Default, J: Defaultimpl<A, B, C, D, E, F, G, H, I, J, K> Default for (A, B, C, D, E, F, G, H, I, J, K) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Default, G: Default, H: Default, I: Default, J: Default, K: Defaultimpl<A, B, C, D, E, F, G, H, I, J, K, L> Default for (A, B, C, D, E, F, G, H, I, J, K, L) where A: Default, B: Default, C: Default, D: Default, E: Default, F: Default, G: Default, H: Default, I: Default, J: Default, K: Default, L: Defaultimpl<T> Default for Box<T> where T: Defaultimpl<T> Default for Box<[T]>impl<T> Default for Weak<T>impl<T> Default for Arc<T> where T: Defaultimpl<T> Default for Rc<T> where T: Defaultimpl<T> Default for Weak<T>impl<T> Default for BinaryHeap<T> where T: Ordimpl<K, V> Default for BTreeMap<K, V> where K: Ordimpl<T> Default for BTreeSet<T> where T: Ordimpl<'a, B> Default for Cow<'a, B> where B: ToOwned + ?Sized, B::Owned: Defaultimpl<T> Default for LinkedList<T>impl Default for Stringimpl<T> Default for Vec<T>impl<T> Default for VecDeque<T>impl<K, V, S> Default for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasher + Defaultimpl Default for RandomStateimpl<T, S> Default for HashSet<T, S> where T: Eq + Hash, S: BuildHasher + Defaultimpl<'a> Default for &'a CStrimpl Default for CStringimpl Default for OsStringimpl<'a> Default for &'a OsStrimpl Default for Condvarimpl<T: ?Sized + Default> Default for Mutex<T>impl<T: Default> Default for RwLock<T>impl Default for ReseedWithDefaultResult is a type that represents either success (Ok) or failure (Err).
See the std::result module documentation for details.
fn is_ok(&self) -> boolReturns true if the result is Ok
Basic usage:
fn main() { let x: Result<i32, &str> = Ok(-3); assert_eq!(x.is_ok(), true); let x: Result<i32, &str> = Err("Some error message"); assert_eq!(x.is_ok(), false); }let x: Result<i32, &str> = Ok(-3); assert_eq!(x.is_ok(), true); let x: Result<i32, &str> = Err("Some error message"); assert_eq!(x.is_ok(), false);
fn is_err(&self) -> boolReturns true if the result is Err
Basic usage:
fn main() { let x: Result<i32, &str> = Ok(-3); assert_eq!(x.is_err(), false); let x: Result<i32, &str> = Err("Some error message"); assert_eq!(x.is_err(), true); }let x: Result<i32, &str> = Ok(-3); assert_eq!(x.is_err(), false); let x: Result<i32, &str> = Err("Some error message"); assert_eq!(x.is_err(), true);
fn as_ref(&self) -> Result<&T, &E>Converts from Result<T, E> to Result<&T, &E>
Produces a new Result, containing a reference
into the original, leaving the original in place.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result<u32, &str> = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); }let x: Result<u32, &str> = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result<u32, &str> = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error"));
fn as_mut(&mut self) -> Result<&mut T, &mut E>Converts from Result<T, E> to Result<&mut T, &mut E>
Basic usage:
fn main() { fn mutate(r: &mut Result<i32, i32>) { match r.as_mut() { Ok(&mut ref mut v) => *v = 42, Err(&mut ref mut e) => *e = 0, } } let mut x: Result<i32, i32> = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result<i32, i32> = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); }fn mutate(r: &mut Result<i32, i32>) { match r.as_mut() { Ok(&mut ref mut v) => *v = 42, Err(&mut ref mut e) => *e = 0, } } let mut x: Result<i32, i32> = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result<i32, i32> = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0);
fn iter(&self) -> Iter<T>Returns an iterator over the possibly contained value.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(7); assert_eq!(x.iter().next(), Some(&7)); let x: Result<u32, &str> = Err("nothing!"); assert_eq!(x.iter().next(), None); }let x: Result<u32, &str> = Ok(7); assert_eq!(x.iter().next(), Some(&7)); let x: Result<u32, &str> = Err("nothing!"); assert_eq!(x.iter().next(), None);
fn iter_mut(&mut self) -> IterMut<T>Returns a mutable iterator over the possibly contained value.
Basic usage:
fn main() { let mut x: Result<u32, &str> = Ok(7); match x.iter_mut().next() { Some(v) => *v = 40, None => {}, } assert_eq!(x, Ok(40)); let mut x: Result<u32, &str> = Err("nothing!"); assert_eq!(x.iter_mut().next(), None); }let mut x: Result<u32, &str> = Ok(7); match x.iter_mut().next() { Some(v) => *v = 40, None => {}, } assert_eq!(x, Ok(40)); let mut x: Result<u32, &str> = Err("nothing!"); assert_eq!(x.iter_mut().next(), None);
fn unwrap(self) -> TUnwraps a result, yielding the content of an Ok.
Panics if the value is an Err, with a panic message provided by the
Err's value.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.unwrap(), 2); }let x: Result<u32, &str> = Ok(2); assert_eq!(x.unwrap(), 2);fn main() { let x: Result<u32, &str> = Err("emergency failure"); x.unwrap(); // panics with `emergency failure` }
let x: Result<u32, &str> = Err("emergency failure"); x.unwrap(); // panics with `emergency failure`
fn expect(self, msg: &str) -> TUnwraps a result, yielding the content of an Ok.
Panics if the value is an Err, with a panic message including the
passed message, and the content of the Err.
Basic usage:
fn main() { let x: Result<u32, &str> = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure` }let x: Result<u32, &str> = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure`
fn unwrap_or(self, optb: T) -> TUnwraps a result, yielding the content of an Ok.
Else it returns optb.
Basic usage:
fn main() { let optb = 2; let x: Result<u32, &str> = Ok(9); assert_eq!(x.unwrap_or(optb), 9); let x: Result<u32, &str> = Err("error"); assert_eq!(x.unwrap_or(optb), optb); }let optb = 2; let x: Result<u32, &str> = Ok(9); assert_eq!(x.unwrap_or(optb), 9); let x: Result<u32, &str> = Err("error"); assert_eq!(x.unwrap_or(optb), optb);
fn unwrap_or_else<F>(self, op: F) -> T where F: FnOnce(E) -> TUnwraps a result, yielding the content of an Ok.
If the value is an Err then it calls op with its value.
Basic usage:
fn main() { fn count(x: &str) -> usize { x.len() } assert_eq!(Ok(2).unwrap_or_else(count), 2); assert_eq!(Err("foo").unwrap_or_else(count), 3); }fn count(x: &str) -> usize { x.len() } assert_eq!(Ok(2).unwrap_or_else(count), 2); assert_eq!(Err("foo").unwrap_or_else(count), 3);
fn unwrap_err(self) -> EUnwraps a result, yielding the content of an Err.
Panics if the value is an Ok, with a custom panic message provided
by the Ok's value.
let x: Result<u32, &str> = Ok(2); x.unwrap_err(); // panics with `2`fn main() { let x: Result<u32, &str> = Err("emergency failure"); assert_eq!(x.unwrap_err(), "emergency failure"); }
let x: Result<u32, &str> = Err("emergency failure"); assert_eq!(x.unwrap_err(), "emergency failure");
fn map<U, F>(self, op: F) -> Result<U, E> where F: FnOnce(T) -> UMaps a Result<T, E> to Result<U, E> by applying a function to a
contained Ok value, leaving an Err value untouched.
This function can be used to compose the results of two functions.
Print the numbers on each line of a string multiplied by two.
fn main() { let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::<i32>().map(|i| i * 2) { Ok(n) => println!("{}", n), Err(..) => {} } } }let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::<i32>().map(|i| i * 2) { Ok(n) => println!("{}", n), Err(..) => {} } }
fn map_err<F, O>(self, op: O) -> Result<T, F> where O: FnOnce(E) -> FMaps a Result<T, E> to Result<T, F> by applying a function to a
contained Err value, leaving an Ok value untouched.
This function can be used to pass through a successful result while handling an error.
Basic usage:
fn main() { fn stringify(x: u32) -> String { format!("error code: {}", x) } let x: Result<u32, u32> = Ok(2); assert_eq!(x.map_err(stringify), Ok(2)); let x: Result<u32, u32> = Err(13); assert_eq!(x.map_err(stringify), Err("error code: 13".to_string())); }fn stringify(x: u32) -> String { format!("error code: {}", x) } let x: Result<u32, u32> = Ok(2); assert_eq!(x.map_err(stringify), Ok(2)); let x: Result<u32, u32> = Err(13); assert_eq!(x.map_err(stringify), Err("error code: 13".to_string()));
fn ok(self) -> Option<T>Converts from Result<T, E> to Option<T>
Converts self into an Option<T>, consuming self,
and discarding the error, if any.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.ok(), None); }let x: Result<u32, &str> = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.ok(), None);
fn err(self) -> Option<E>Converts from Result<T, E> to Option<E>
Converts self into an Option<E>, consuming self,
and discarding the success value, if any.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(2); assert_eq!(x.err(), None); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.err(), Some("Nothing here")); }let x: Result<u32, &str> = Ok(2); assert_eq!(x.err(), None); let x: Result<u32, &str> = Err("Nothing here"); assert_eq!(x.err(), Some("Nothing here"));
fn and<U>(self, res: Result<U, E>) -> Result<U, E>Returns res if the result is Ok, otherwise returns the Err value of self.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result<u32, &str> = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result<u32, &str> = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result<u32, &str> = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); }let x: Result<u32, &str> = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result<u32, &str> = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result<u32, &str> = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result<u32, &str> = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type"));
fn and_then<U, F>(self, op: F) -> Result<U, E> where F: FnOnce(T) -> Result<U, E>Calls op if the result is Ok, otherwise returns the Err value of self.
This function can be used for control flow based on result values.
Basic usage:
fn main() { fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).and_then(sq).and_then(sq), Ok(16)); assert_eq!(Ok(2).and_then(sq).and_then(err), Err(4)); assert_eq!(Ok(2).and_then(err).and_then(sq), Err(2)); assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3)); }fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).and_then(sq).and_then(sq), Ok(16)); assert_eq!(Ok(2).and_then(sq).and_then(err), Err(4)); assert_eq!(Ok(2).and_then(err).and_then(sq), Err(2)); assert_eq!(Err(3).and_then(sq).and_then(sq), Err(3));
fn or<F>(self, res: Result<T, F>) -> Result<T, F>Returns res if the result is Err, otherwise returns the Ok value of self.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("early error"); let y: Result<u32, &str> = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("not a 2"); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Ok(100); assert_eq!(x.or(y), Ok(2)); }let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("early error"); let y: Result<u32, &str> = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result<u32, &str> = Err("not a 2"); let y: Result<u32, &str> = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result<u32, &str> = Ok(2); let y: Result<u32, &str> = Ok(100); assert_eq!(x.or(y), Ok(2));
fn or_else<F, O>(self, op: O) -> Result<T, F> where O: FnOnce(E) -> Result<T, F>Calls op if the result is Err, otherwise returns the Ok value of self.
This function can be used for control flow based on result values.
Basic usage:
fn main() { fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2)); assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2)); assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9)); assert_eq!(Err(3).or_else(err).or_else(err), Err(3)); }fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) } fn err(x: u32) -> Result<u32, u32> { Err(x) } assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2)); assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2)); assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9)); assert_eq!(Err(3).or_else(err).or_else(err), Err(3));
impl<T, E> Hash for Result<T, E> where E: Hash, T: Hashfn hash<__HTE>(&self, __arg_0: &mut __HTE) where __HTE: HasherFeeds this value into the state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: HasherFeeds a slice of this type into the state provided.
impl<T, E> PartialOrd<Result<T, E>> for Result<T, E> where E: PartialOrd<E>, T: PartialOrd<T>fn partial_cmp(&self, __arg_0: &Result<T, E>) -> Option<Ordering>This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, __arg_0: &Result<T, E>) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, __arg_0: &Result<T, E>) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, __arg_0: &Result<T, E>) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, __arg_0: &Result<T, E>) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl<T, E> IntoIterator for Result<T, E>type Item = TThe type of the elements being iterated over.
type IntoIter = IntoIter<T>Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<T>Returns a consuming iterator over the possibly contained value.
Basic usage:
fn main() { let x: Result<u32, &str> = Ok(5); let v: Vec<u32> = x.into_iter().collect(); assert_eq!(v, [5]); let x: Result<u32, &str> = Err("nothing!"); let v: Vec<u32> = x.into_iter().collect(); assert_eq!(v, []); }let x: Result<u32, &str> = Ok(5); let v: Vec<u32> = x.into_iter().collect(); assert_eq!(v, [5]); let x: Result<u32, &str> = Err("nothing!"); let v: Vec<u32> = x.into_iter().collect(); assert_eq!(v, []);
impl<'a, T, E> IntoIterator for &'a Result<T, E>type Item = &'a TThe type of the elements being iterated over.
type IntoIter = Iter<'a, T>Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, T>Creates an iterator from a value. Read more
impl<'a, T, E> IntoIterator for &'a mut Result<T, E>impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E> where V: FromIterator<A>fn from_iter<I>(iter: I) -> Result<V, E> where I: IntoIterator<Item=Result<A, E>>Takes each element in the Iterator: if it is an Err, no further
elements are taken, and the Err is returned. Should no Err occur, a
container with the values of each Result is returned.
Here is an example which increments every integer in a vector, checking for overflow:
fn main() { use std::u32; let v = vec!(1, 2); let res: Result<Vec<u32>, &'static str> = v.iter().map(|&x: &u32| if x == u32::MAX { Err("Overflow!") } else { Ok(x + 1) } ).collect(); assert!(res == Ok(vec!(2, 3))); }use std::u32; let v = vec!(1, 2); let res: Result<Vec<u32>, &'static str> = v.iter().map(|&x: &u32| if x == u32::MAX { Err("Overflow!") } else { Ok(x + 1) } ).collect(); assert!(res == Ok(vec!(2, 3)));
pub trait Index<Idx> where Idx: ?Sized { type Output: ?Sized; fn index(&self, index: Idx) -> &Self::Output; }
The Index trait is used to specify the functionality of indexing operations
like arr[idx] when used in an immutable context.
A trivial implementation of Index. When Foo[Bar] happens, it ends up
calling index, and therefore, main prints Indexing!.
use std::ops::Index; #[derive(Copy, Clone)] struct Foo; struct Bar; impl Index<Bar> for Foo { type Output = Foo; fn index<'a>(&'a self, _index: Bar) -> &'a Foo { println!("Indexing!"); self } } fn main() { Foo[Bar]; }
impl<T> Index<usize> for [T]impl<T> Index<Range<usize>> for [T]impl<T> Index<RangeTo<usize>> for [T]impl<T> Index<RangeFrom<usize>> for [T]impl<T> Index<RangeFull> for [T]impl<T> Index<RangeInclusive<usize>> for [T]impl<T> Index<RangeToInclusive<usize>> for [T]impl Index<Range<usize>> for strimpl Index<RangeTo<usize>> for strimpl Index<RangeFrom<usize>> for strimpl Index<RangeFull> for strimpl Index<RangeInclusive<usize>> for strimpl Index<RangeToInclusive<usize>> for strimpl<'a, K, Q, V> Index<&'a Q> for BTreeMap<K, V> where K: Ord + Borrow<Q>, Q: Ord + ?Sizedimpl Index<Range<usize>> for Stringimpl Index<RangeTo<usize>> for Stringimpl Index<RangeFrom<usize>> for Stringimpl Index<RangeFull> for Stringimpl Index<RangeInclusive<usize>> for Stringimpl Index<RangeToInclusive<usize>> for Stringimpl<T> Index<usize> for Vec<T>impl<T> Index<Range<usize>> for Vec<T>impl<T> Index<RangeTo<usize>> for Vec<T>impl<T> Index<RangeFrom<usize>> for Vec<T>impl<T> Index<RangeFull> for Vec<T>impl<T> Index<RangeInclusive<usize>> for Vec<T>impl<T> Index<RangeToInclusive<usize>> for Vec<T>impl<A> Index<usize> for VecDeque<A>impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S> where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash, S: BuildHasherimpl Index<RangeFull> for CStringimpl Index<RangeFull> for OsStringpub trait From<T> {
fn from(T) -> Self;
}Construct Self via a conversion.
Note: this trait must not fail. If the conversion can fail, use TryFrom or a dedicated
method which returns an Option<T> or a Result<T, E>.
String implements From<&str>:
let string = "hello".to_string(); let other_string = String::from("hello"); assert_eq!(string, other_string);
From<T> for U implies Into<U> for Tfrom() is reflexive, which means that From<T> for T is implementedfn from(T) -> SelfPerforms the conversion.
impl From<u8> for u16impl From<u8> for u32impl From<u8> for u64impl From<u8> for usizeimpl From<u16> for u32impl From<u16> for u64impl From<u32> for u64impl From<i8> for i16impl From<i8> for i32impl From<i8> for i64impl From<i8> for isizeimpl From<i16> for i32impl From<i16> for i64impl From<i32> for i64impl From<u8> for i16impl From<u8> for i32impl From<u8> for i64impl From<u16> for i32impl From<u16> for i64impl From<u32> for i64impl From<i8> for f32impl From<i8> for f64impl From<i16> for f32impl From<i16> for f64impl From<i32> for f64impl From<u8> for f32impl From<u8> for f64impl From<u16> for f32impl From<u16> for f64impl From<u32> for f64impl From<f32> for f64impl<T> From<T> for Timpl<T> From<T> for Box<T>impl<T> From<T> for Arc<T>impl<T> From<T> for Rc<T>impl<T> From<Vec<T>> for BinaryHeap<T> where T: Ordimpl<T> From<BinaryHeap<T>> for Vec<T>impl<'a> From<&'a str> for Stringimpl<'a> From<&'a str> for Cow<'a, str>impl<'a> From<String> for Cow<'a, str>impl<'a, T> From<&'a [T]> for Vec<T> where T: Cloneimpl<'a> From<&'a str> for Vec<u8>impl<'a, T> From<&'a [T]> for Cow<'a, [T]> where T: Cloneimpl<'a, T> From<Vec<T>> for Cow<'a, [T]> where T: Cloneimpl<T> From<Vec<T>> for VecDeque<T>impl<T> From<VecDeque<T>> for Vec<T>impl<'a, E: Error + 'a> From<E> for Box<Error + 'a>impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a>impl From<String> for Box<Error + Send + Sync>impl From<String> for Box<Error>impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a>impl<'a> From<&'a str> for Box<Error>impl From<CString> for Vec<u8>impl From<NulError> for Errorimpl<'a> From<&'a CStr> for CStringimpl From<String> for OsStringimpl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for OsStringimpl<W> From<IntoInnerError<W>> for Errorimpl From<Ipv4Addr> for u32impl From<u32> for Ipv4Addrimpl From<[u8; 4]> for Ipv4Addrimpl From<[u8; 16]> for Ipv6Addrimpl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for PathBufimpl From<OsString> for PathBufimpl From<String> for PathBufimpl<'a> From<&'a Path> for Cow<'a, Path>impl<'a> From<PathBuf> for Cow<'a, Path>impl<T> From<PoisonError<T>> for TryLockError<T>pub trait Borrow<Borrowed> where Borrowed: ?Sized { fn borrow(&self) -> &Borrowed; }
A trait for borrowing data.
In general, there may be several ways to "borrow" a piece of data. The
typical ways of borrowing a type T are &T (a shared borrow) and &mut T
(a mutable borrow). But types like Vec<T> provide additional kinds of
borrows: the borrowed slices &[T] and &mut [T].
When writing generic code, it is often desirable to abstract over all ways
of borrowing data from a given type. That is the role of the Borrow
trait: if T: Borrow<U>, then &U can be borrowed from &T. A given
type can be borrowed as multiple different types. In particular, Vec<T>: Borrow<Vec<T>> and Vec<T>: Borrow<[T]>.
If you are implementing Borrow and both Self and Borrowed implement
Hash, Eq, and/or Ord, they must produce the same result.
Borrow is very similar to, but different than, AsRef. See
the book for more.
fn borrow(&self) -> &BorrowedImmutably borrows from an owned value.
use std::borrow::Borrow; fn check<T: Borrow<str>>(s: T) { assert_eq!("Hello", s.borrow()); } let s = "Hello".to_string(); check(s); let s = "Hello"; check(s);
impl<T> Borrow<T> for T where T: ?Sizedimpl<'a, T> Borrow<T> for &'a T where T: ?Sizedimpl<'a, T> Borrow<T> for &'a mut T where T: ?Sizedimpl<T> Borrow<[T]> for [T; 0]impl<T> Borrow<[T]> for [T; 1]impl<T> Borrow<[T]> for [T; 2]impl<T> Borrow<[T]> for [T; 3]impl<T> Borrow<[T]> for [T; 4]impl<T> Borrow<[T]> for [T; 5]impl<T> Borrow<[T]> for [T; 6]impl<T> Borrow<[T]> for [T; 7]impl<T> Borrow<[T]> for [T; 8]impl<T> Borrow<[T]> for [T; 9]impl<T> Borrow<[T]> for [T; 10]impl<T> Borrow<[T]> for [T; 11]impl<T> Borrow<[T]> for [T; 12]impl<T> Borrow<[T]> for [T; 13]impl<T> Borrow<[T]> for [T; 14]impl<T> Borrow<[T]> for [T; 15]impl<T> Borrow<[T]> for [T; 16]impl<T> Borrow<[T]> for [T; 17]impl<T> Borrow<[T]> for [T; 18]impl<T> Borrow<[T]> for [T; 19]impl<T> Borrow<[T]> for [T; 20]impl<T> Borrow<[T]> for [T; 21]impl<T> Borrow<[T]> for [T; 22]impl<T> Borrow<[T]> for [T; 23]impl<T> Borrow<[T]> for [T; 24]impl<T> Borrow<[T]> for [T; 25]impl<T> Borrow<[T]> for [T; 26]impl<T> Borrow<[T]> for [T; 27]impl<T> Borrow<[T]> for [T; 28]impl<T> Borrow<[T]> for [T; 29]impl<T> Borrow<[T]> for [T; 30]impl<T> Borrow<[T]> for [T; 31]impl<T> Borrow<[T]> for [T; 32]impl<T> Borrow<T> for Box<T> where T: ?Sizedimpl<T> Borrow<T> for Arc<T> where T: ?Sizedimpl<T> Borrow<T> for Rc<T> where T: ?Sizedimpl<'a, B> Borrow<B> for Cow<'a, B> where B: ToOwned + ?Sized, B::Owned: 'aimpl<T> Borrow<[T]> for Vec<T>impl Borrow<str> for Stringimpl Borrow<CStr> for CStringimpl Borrow<OsStr> for OsStringimpl Borrow<Path> for PathBufpub trait BorrowMut<Borrowed>: Borrow<Borrowed> where Borrowed: ?Sized { fn borrow_mut(&mut self) -> &mut Borrowed; }
A trait for mutably borrowing data.
Similar to Borrow, but for mutable borrows.
fn borrow_mut(&mut self) -> &mut BorrowedMutably borrows from an owned value.
use std::borrow::BorrowMut; fn check<T: BorrowMut<[i32]>>(mut v: T) { assert_eq!(&mut [1, 2, 3], v.borrow_mut()); } let v = vec![1, 2, 3]; check(v);
impl<T> BorrowMut<T> for T where T: ?Sizedimpl<'a, T> BorrowMut<T> for &'a mut T where T: ?Sizedimpl<T> BorrowMut<[T]> for [T; 0]impl<T> BorrowMut<[T]> for [T; 1]impl<T> BorrowMut<[T]> for [T; 2]impl<T> BorrowMut<[T]> for [T; 3]impl<T> BorrowMut<[T]> for [T; 4]impl<T> BorrowMut<[T]> for [T; 5]impl<T> BorrowMut<[T]> for [T; 6]impl<T> BorrowMut<[T]> for [T; 7]impl<T> BorrowMut<[T]> for [T; 8]impl<T> BorrowMut<[T]> for [T; 9]impl<T> BorrowMut<[T]> for [T; 10]impl<T> BorrowMut<[T]> for [T; 11]impl<T> BorrowMut<[T]> for [T; 12]impl<T> BorrowMut<[T]> for [T; 13]impl<T> BorrowMut<[T]> for [T; 14]impl<T> BorrowMut<[T]> for [T; 15]impl<T> BorrowMut<[T]> for [T; 16]impl<T> BorrowMut<[T]> for [T; 17]impl<T> BorrowMut<[T]> for [T; 18]impl<T> BorrowMut<[T]> for [T; 19]impl<T> BorrowMut<[T]> for [T; 20]impl<T> BorrowMut<[T]> for [T; 21]impl<T> BorrowMut<[T]> for [T; 22]impl<T> BorrowMut<[T]> for [T; 23]impl<T> BorrowMut<[T]> for [T; 24]impl<T> BorrowMut<[T]> for [T; 25]impl<T> BorrowMut<[T]> for [T; 26]impl<T> BorrowMut<[T]> for [T; 27]impl<T> BorrowMut<[T]> for [T; 28]impl<T> BorrowMut<[T]> for [T; 29]impl<T> BorrowMut<[T]> for [T; 30]impl<T> BorrowMut<[T]> for [T; 31]impl<T> BorrowMut<[T]> for [T; 32]impl<T> BorrowMut<T> for Box<T> where T: ?Sizedimpl<T> BorrowMut<[T]> for Vec<T>pub trait IndexMut<Idx>: Index<Idx> where Idx: ?Sized { fn index_mut(&mut self, index: Idx) -> &mut Self::Output; }
The IndexMut trait is used to specify the functionality of indexing
operations like arr[idx], when used in a mutable context.
A trivial implementation of IndexMut. When Foo[Bar] happens, it ends up
calling index_mut, and therefore, main prints Indexing!.
use std::ops::{Index, IndexMut}; #[derive(Copy, Clone)] struct Foo; struct Bar; impl Index<Bar> for Foo { type Output = Foo; fn index<'a>(&'a self, _index: Bar) -> &'a Foo { self } } impl IndexMut<Bar> for Foo { fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo { println!("Indexing!"); self } } fn main() { &mut Foo[Bar]; }
fn index_mut(&mut self, index: Idx) -> &mut Self::OutputThe method for the indexing (Foo[Bar]) operation
impl<T> IndexMut<usize> for [T]impl<T> IndexMut<Range<usize>> for [T]impl<T> IndexMut<RangeTo<usize>> for [T]impl<T> IndexMut<RangeFrom<usize>> for [T]impl<T> IndexMut<RangeFull> for [T]impl<T> IndexMut<RangeInclusive<usize>> for [T]impl<T> IndexMut<RangeToInclusive<usize>> for [T]impl IndexMut<Range<usize>> for strimpl IndexMut<RangeTo<usize>> for strimpl IndexMut<RangeFrom<usize>> for strimpl IndexMut<RangeFull> for strimpl IndexMut<RangeInclusive<usize>> for strimpl IndexMut<RangeToInclusive<usize>> for strimpl IndexMut<Range<usize>> for Stringimpl IndexMut<RangeTo<usize>> for Stringimpl IndexMut<RangeFrom<usize>> for Stringimpl IndexMut<RangeFull> for Stringimpl IndexMut<RangeInclusive<usize>> for Stringimpl IndexMut<RangeToInclusive<usize>> for Stringimpl<T> IndexMut<usize> for Vec<T>impl<T> IndexMut<Range<usize>> for Vec<T>impl<T> IndexMut<RangeTo<usize>> for Vec<T>impl<T> IndexMut<RangeFrom<usize>> for Vec<T>impl<T> IndexMut<RangeFull> for Vec<T>impl<T> IndexMut<RangeInclusive<usize>> for Vec<T>impl<T> IndexMut<RangeToInclusive<usize>> for Vec<T>impl<A> IndexMut<usize> for VecDeque<A>pub trait Deref {
type Target: ?Sized;
fn deref(&self) -> &Self::Target;
}The Deref trait is used to specify the functionality of dereferencing
operations, like *v.
Deref also enables 'Deref coercions'.
A struct with a single field which is accessible via dereferencing the struct.
use std::ops::Deref; struct DerefExample<T> { value: T } impl<T> Deref for DerefExample<T> { type Target = T; fn deref(&self) -> &T { &self.value } } fn main() { let x = DerefExample { value: 'a' }; assert_eq!('a', *x); }use std::ops::Deref; struct DerefExample<T> { value: T } impl<T> Deref for DerefExample<T> { type Target = T; fn deref(&self) -> &T { &self.value } } fn main() { let x = DerefExample { value: 'a' }; assert_eq!('a', *x); }
impl<T> Deref for NonZero<T> where T: Zeroableimpl<T> Deref for Unique<T> where T: ?Sizedimpl<T> Deref for Shared<T> where T: ?Sizedimpl<'a, T> Deref for &'a T where T: ?Sizedimpl<'a, T> Deref for &'a mut T where T: ?Sizedimpl<'b, T> Deref for Ref<'b, T> where T: ?Sizedimpl<'b, T> Deref for RefMut<'b, T> where T: ?Sizedimpl<T> Deref for Box<T> where T: ?Sizedimpl<T> Deref for Arc<T> where T: ?Sizedimpl<T> Deref for Rc<T> where T: ?Sizedimpl<'a, T> Deref for PeekMut<'a, T> where T: Ordimpl<'a, B> Deref for Cow<'a, B> where B: ToOwned + ?Sizedimpl Deref for Stringimpl<T> Deref for Vec<T>impl Deref for CStringimpl Deref for OsStringimpl<T> Deref for AssertUnwindSafe<T>impl Deref for PathBufimpl<'mutex, T: ?Sized> Deref for MutexGuard<'mutex, T>impl<'rwlock, T: ?Sized> Deref for RwLockReadGuard<'rwlock, T>impl<'rwlock, T: ?Sized> Deref for RwLockWriteGuard<'rwlock, T>pub trait DerefMut: Deref { fn deref_mut(&mut self) -> &mut Self::Target; }
The DerefMut trait is used to specify the functionality of dereferencing
mutably like *v = 1;
DerefMut also enables 'Deref coercions'.
A struct with a single field which is modifiable via dereferencing the struct.
use std::ops::{Deref, DerefMut}; struct DerefMutExample<T> { value: T } impl<T> Deref for DerefMutExample<T> { type Target = T; fn deref<'a>(&'a self) -> &'a T { &self.value } } impl<T> DerefMut for DerefMutExample<T> { fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut self.value } } fn main() { let mut x = DerefMutExample { value: 'a' }; *x = 'b'; assert_eq!('b', *x); }use std::ops::{Deref, DerefMut}; struct DerefMutExample<T> { value: T } impl<T> Deref for DerefMutExample<T> { type Target = T; fn deref<'a>(&'a self) -> &'a T { &self.value } } impl<T> DerefMut for DerefMutExample<T> { fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut self.value } } fn main() { let mut x = DerefMutExample { value: 'a' }; *x = 'b'; assert_eq!('b', *x); }
impl<'a, T> DerefMut for &'a mut T where T: ?Sizedimpl<'b, T> DerefMut for RefMut<'b, T> where T: ?Sizedimpl<T> DerefMut for Box<T> where T: ?Sizedimpl<'a, T> DerefMut for PeekMut<'a, T> where T: Ordimpl DerefMut for Stringimpl<T> DerefMut for Vec<T>impl<T> DerefMut for AssertUnwindSafe<T>impl<'mutex, T: ?Sized> DerefMut for MutexGuard<'mutex, T>impl<'rwlock, T: ?Sized> DerefMut for RwLockWriteGuard<'rwlock, T>pub trait Drop {
fn drop(&mut self);
}The Drop trait is used to run some code when a value goes out of scope.
This is sometimes called a 'destructor'.
A trivial implementation of Drop. The drop method is called when _x
goes out of scope, and therefore main prints Dropping!.
struct HasDrop; impl Drop for HasDrop { fn drop(&mut self) { println!("Dropping!"); } } fn main() { let _x = HasDrop; }
fn drop(&mut self)A method called when the value goes out of scope.
When this method has been called, self has not yet been deallocated.
If it were, self would be a dangling reference.
After this function is over, the memory of self will be deallocated.
Given that a panic! will call drop() as it unwinds, any panic! in
a drop() implementation will likely abort.
impl<T> Drop for IntermediateBox<T> where T: ?Sizedimpl<T> Drop for Arc<T> where T: ?Sizedimpl<T> Drop for Weak<T> where T: ?Sizedimpl<T> Drop for Rc<T> where T: ?Sizedimpl<T> Drop for Weak<T> where T: ?Sizedimpl<T> Drop for RawVec<T>impl<'a, T> Drop for PeekMut<'a, T> where T: Ordimpl<K, V> Drop for BTreeMap<K, V>impl<K, V> Drop for IntoIter<K, V>impl<T> Drop for LinkedList<T>impl<'a> Drop for Drain<'a>impl<T> Drop for Vec<T>impl<T> Drop for IntoIter<T>impl<'a, T> Drop for Drain<'a, T>impl<T> Drop for VecDeque<T>impl<'a, T> Drop for Drain<'a, T> where T: 'aimpl<W: Write> Drop for BufWriter<W>impl Drop for Selectimpl<'rx, T: Send> Drop for Handle<'rx, T>impl<T> Drop for Sender<T>impl<T> Drop for SyncSender<T>impl<T> Drop for Receiver<T>impl Drop for Condvarimpl<T: ?Sized> Drop for Mutex<T>impl<'a, T: ?Sized> Drop for MutexGuard<'a, T>impl<T: ?Sized> Drop for RwLock<T>impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T>impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T>pub trait AsRef<T> where T: ?Sized { fn as_ref(&self) -> &T; }
A cheap, reference-to-reference conversion.
AsRef is very similar to, but different than, Borrow. See
the book for more.
Note: this trait must not fail. If the conversion can fail, use a dedicated method which
returns an Option<T> or a Result<T, E>.
Both String and &str implement AsRef<str>:
fn is_hello<T: AsRef<str>>(s: T) { assert_eq!("hello", s.as_ref()); } let s = "hello"; is_hello(s); let s = "hello".to_string(); is_hello(s);
AsRef auto-dereference if the inner type is a reference or a mutable
reference (eg: foo.as_ref() will work the same if foo has type &mut Foo or &&mut Foo)fn as_ref(&self) -> &TPerforms the conversion.
impl<'a, T, U> AsRef<U> for &'a T where T: AsRef<U> + ?Sized, U: ?Sizedimpl<'a, T, U> AsRef<U> for &'a mut T where T: AsRef<U> + ?Sized, U: ?Sizedimpl<T> AsRef<[T]> for [T]impl AsRef<str> for strimpl<T> AsRef<[T]> for [T; 0]impl<T> AsRef<[T]> for [T; 1]impl<T> AsRef<[T]> for [T; 2]impl<T> AsRef<[T]> for [T; 3]impl<T> AsRef<[T]> for [T; 4]impl<T> AsRef<[T]> for [T; 5]impl<T> AsRef<[T]> for [T; 6]impl<T> AsRef<[T]> for [T; 7]impl<T> AsRef<[T]> for [T; 8]impl<T> AsRef<[T]> for [T; 9]impl<T> AsRef<[T]> for [T; 10]impl<T> AsRef<[T]> for [T; 11]impl<T> AsRef<[T]> for [T; 12]impl<T> AsRef<[T]> for [T; 13]impl<T> AsRef<[T]> for [T; 14]impl<T> AsRef<[T]> for [T; 15]impl<T> AsRef<[T]> for [T; 16]impl<T> AsRef<[T]> for [T; 17]impl<T> AsRef<[T]> for [T; 18]impl<T> AsRef<[T]> for [T; 19]impl<T> AsRef<[T]> for [T; 20]impl<T> AsRef<[T]> for [T; 21]impl<T> AsRef<[T]> for [T; 22]impl<T> AsRef<[T]> for [T; 23]impl<T> AsRef<[T]> for [T; 24]impl<T> AsRef<[T]> for [T; 25]impl<T> AsRef<[T]> for [T; 26]impl<T> AsRef<[T]> for [T; 27]impl<T> AsRef<[T]> for [T; 28]impl<T> AsRef<[T]> for [T; 29]impl<T> AsRef<[T]> for [T; 30]impl<T> AsRef<[T]> for [T; 31]impl<T> AsRef<[T]> for [T; 32]impl AsRef<[u8]> for strimpl<T> AsRef<T> for Box<T> where T: ?Sizedimpl<T> AsRef<T> for Arc<T> where T: ?Sizedimpl<T> AsRef<T> for Rc<T> where T: ?Sizedimpl<'a, T> AsRef<T> for Cow<'a, T> where T: ToOwned + ?Sizedimpl AsRef<str> for Stringimpl AsRef<[u8]> for Stringimpl<T> AsRef<Vec<T>> for Vec<T>impl<T> AsRef<[T]> for Vec<T>impl AsRef<CStr> for CStrimpl AsRef<CStr> for CStringimpl AsRef<OsStr> for OsStrimpl AsRef<OsStr> for OsStringimpl AsRef<OsStr> for strimpl AsRef<OsStr> for Stringimpl<'a> AsRef<OsStr> for Component<'a>impl<'a> AsRef<Path> for Components<'a>impl<'a> AsRef<OsStr> for Components<'a>impl<'a> AsRef<Path> for Iter<'a>impl<'a> AsRef<OsStr> for Iter<'a>impl AsRef<OsStr> for PathBufimpl AsRef<OsStr> for Pathimpl AsRef<Path> for Pathimpl AsRef<Path> for OsStrimpl<'a> AsRef<Path> for Cow<'a, OsStr>impl AsRef<Path> for OsStringimpl AsRef<Path> for strimpl AsRef<Path> for Stringimpl AsRef<Path> for PathBufpub trait AsMut<T> where T: ?Sized { fn as_mut(&mut self) -> &mut T; }
A cheap, mutable reference-to-mutable reference conversion.
Note: this trait must not fail. If the conversion can fail, use a dedicated method which
returns an Option<T> or a Result<T, E>.
AsMut auto-dereference if the inner type is a reference or a mutable
reference (eg: foo.as_ref() will work the same if foo has type &mut Foo or &&mut Foo)fn as_mut(&mut self) -> &mut TPerforms the conversion.
impl<'a, T, U> AsMut<U> for &'a mut T where T: AsMut<U> + ?Sized, U: ?Sizedimpl<T> AsMut<[T]> for [T]impl<T> AsMut<[T]> for [T; 0]impl<T> AsMut<[T]> for [T; 1]impl<T> AsMut<[T]> for [T; 2]impl<T> AsMut<[T]> for [T; 3]impl<T> AsMut<[T]> for [T; 4]impl<T> AsMut<[T]> for [T; 5]impl<T> AsMut<[T]> for [T; 6]impl<T> AsMut<[T]> for [T; 7]impl<T> AsMut<[T]> for [T; 8]impl<T> AsMut<[T]> for [T; 9]impl<T> AsMut<[T]> for [T; 10]impl<T> AsMut<[T]> for [T; 11]impl<T> AsMut<[T]> for [T; 12]impl<T> AsMut<[T]> for [T; 13]impl<T> AsMut<[T]> for [T; 14]impl<T> AsMut<[T]> for [T; 15]impl<T> AsMut<[T]> for [T; 16]impl<T> AsMut<[T]> for [T; 17]impl<T> AsMut<[T]> for [T; 18]impl<T> AsMut<[T]> for [T; 19]impl<T> AsMut<[T]> for [T; 20]impl<T> AsMut<[T]> for [T; 21]impl<T> AsMut<[T]> for [T; 22]impl<T> AsMut<[T]> for [T; 23]impl<T> AsMut<[T]> for [T; 24]impl<T> AsMut<[T]> for [T; 25]impl<T> AsMut<[T]> for [T; 26]impl<T> AsMut<[T]> for [T; 27]impl<T> AsMut<[T]> for [T; 28]impl<T> AsMut<[T]> for [T; 29]impl<T> AsMut<[T]> for [T; 30]impl<T> AsMut<[T]> for [T; 31]impl<T> AsMut<[T]> for [T; 32]impl<T> AsMut<T> for Box<T> where T: ?Sizedimpl<T> AsMut<Vec<T>> for Vec<T>impl<T> AsMut<[T]> for Vec<T>pub trait Extend<A> {
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item=A>;
}Extend a collection with the contents of an iterator.
Iterators produce a series of values, and collections can also be thought
of as a series of values. The Extend trait bridges this gap, allowing you
to extend a collection by including the contents of that iterator.
Basic usage:
fn main() { // You can extend a String with some chars: let mut message = String::from("The first three letters are: "); message.extend(&['a', 'b', 'c']); assert_eq!("abc", &message[29..32]); }// You can extend a String with some chars: let mut message = String::from("The first three letters are: "); message.extend(&['a', 'b', 'c']); assert_eq!("abc", &message[29..32]);
Implementing Extend:
// A sample collection, that's just a wrapper over Vec<T> #[derive(Debug)] struct MyCollection(Vec<i32>); // Let's give it some methods so we can create one and add things // to it. impl MyCollection { fn new() -> MyCollection { MyCollection(Vec::new()) } fn add(&mut self, elem: i32) { self.0.push(elem); } } // since MyCollection has a list of i32s, we implement Extend for i32 impl Extend<i32> for MyCollection { // This is a bit simpler with the concrete type signature: we can call // extend on anything which can be turned into an Iterator which gives // us i32s. Because we need i32s to put into MyCollection. fn extend<T: IntoIterator<Item=i32>>(&mut self, iter: T) { // The implementation is very straightforward: loop through the // iterator, and add() each element to ourselves. for elem in iter { self.add(elem); } } } let mut c = MyCollection::new(); c.add(5); c.add(6); c.add(7); // let's extend our collection with three more numbers c.extend(vec![1, 2, 3]); // we've added these elements onto the end assert_eq!("MyCollection([5, 6, 7, 1, 2, 3])", format!("{:?}", c));
fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item=A>Extends a collection with the contents of an iterator.
As this is the only method for this trait, the trait-level docs contain more details.
Basic usage:
fn main() { // You can extend a String with some chars: let mut message = String::from("abc"); message.extend(['d', 'e', 'f'].iter()); assert_eq!("abcdef", &message); }// You can extend a String with some chars: let mut message = String::from("abc"); message.extend(['d', 'e', 'f'].iter()); assert_eq!("abcdef", &message);
impl<T> Extend<T> for BinaryHeap<T> where T: Ordimpl<'a, T> Extend<&'a T> for BinaryHeap<T> where T: Copy + 'a + Ordimpl<K, V> Extend<(K, V)> for BTreeMap<K, V> where K: Ordimpl<'a, K, V> Extend<(&'a K, &'a V)> for BTreeMap<K, V> where K: Copy + Ord, V: Copyimpl<T> Extend<T> for BTreeSet<T> where T: Ordimpl<'a, T> Extend<&'a T> for BTreeSet<T> where T: Copy + 'a + Ordimpl<E> Extend<E> for EnumSet<E> where E: CLikeimpl<'a, E> Extend<&'a E> for EnumSet<E> where E: Copy + 'a + CLikeimpl<A> Extend<A> for LinkedList<A>impl<'a, T> Extend<&'a T> for LinkedList<T> where T: Copy + 'aimpl Extend<char> for Stringimpl<'a> Extend<&'a char> for Stringimpl<'a> Extend<&'a str> for Stringimpl Extend<String> for Stringimpl<T> Extend<T> for Vec<T>impl<'a, T> Extend<&'a T> for Vec<T> where T: Copy + 'aimpl<A> Extend<A> for VecDeque<A>impl<'a, T> Extend<&'a T> for VecDeque<T> where T: Copy + 'aimpl<K, V, S> Extend<(K, V)> for HashMap<K, V, S> where K: Eq + Hash, S: BuildHasherimpl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S> where K: Eq + Hash + Copy, V: Copy, S: BuildHasherimpl<T, S> Extend<T> for HashSet<T, S> where T: Eq + Hash, S: BuildHasherimpl<'a, T, S> Extend<&'a T> for HashSet<T, S> where T: 'a + Eq + Hash + Copy, S: BuildHasherimpl<P: AsRef<Path>> Extend<P> for PathBuf