0% found this document useful (0 votes)
13 views

Basics - Rust Cheat Sheet

Uploaded by

saadiqah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Basics - Rust Cheat Sheet

Uploaded by

saadiqah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Option<T> Iterator<Item = T> &[T] &[u8]

// To inner type // Mapping and filtering // Splitting to iterator // ASCII


=> unwrap () -> T => map (( T) -> U) -> Iterator<Item = U> => split ((&T) -> bool) -> Iterator<Item = &[T]> => is_ascii () -> bool
=> unwrap_or (T) -> T => filter ((&T) -> bool) -> Iterator<Item = T> => rsplit ((&T) -> bool) -> Iterator<Item = &[T]> => eq_ignore_ascii_case (&[u8]) -> bool
=> unwrap_or_else (() -> T) -> T => filter_map (( T) -> Option<U>) -> Iterator<Item = U> => splitn (usize, (&T) -> bool) -> Iterator<Item = &[T]> => to_ascii_uppercase () -> Vec<u8>
=> unwrap_or_default () -> T where T: Default => rsplitn (usize, (&T) -> bool) -> Iterator<Item = &[T]> => to_ascii_lowercase () -> Vec<u8>
// Collecting and folding
=> expect (&str) -> T
=> fold (S, (S, T) -> S) -> S // Splitting at position
// Converting to another type => collect () -> B where B: FromIterator<T> => split_at (usize) -> (&[T], &[T]) &mut [u8]
=> map ((T) -> U) -> Option<U> => partition ((&T) -> bool) -> (B, B) where B: Default + Extend<T> => split_first () -> Option<(&T, &[T])>
// ASCII
=> map_or (U, (T) -> U) -> U => split_last () -> Option<(&T, &[T])>
// Counting and enumerating => make_ascii_uppercase ()
=> map_or_else (() -> U, (T) -> U) -> U
=> count () -> usize // Chunks and windows => make_ascii_lowercase ()
// To Result => last () -> Option<T> => chunks (usize) -> Iterator<Item = &[T]>
=> ok_or (E) -> Result<T, E>
=> ok_or_else (() -> E) -> Result<T, E>
=> enumerate () -> Iterator<Item = (usize, T)> => chunks_exact (usize) -> Iterator<Item = &[T]>
=> rchunks (usize) -> Iterator<Item = &[T]>
str
// Combining with other iterators => rchunks_exact (usize) -> Iterator<Item = &[T]>
// Bytes
// Conditioning => zip (IntoIterator<Item = U>) -> Iterator<Item = (T, U)>
=> windows (usize) -> Iterator<Item = &[T]>
:: from_utf8 (&[u8]) -> Result<&str, Utf8Error>
=> filter ((&T) -> bool) -> Option<T> => chain (IntoIterator<Item = T>) -> Iterator<Item = T>
:: from_utf8_mut (&mut [u8]) -> Result<&mut str, Utf8Error>
=> and (Option<U>) -> Option<U> // Matching
// Flattening
=> and_then ((T) -> Option<U>) -> Option<U> => contains (&T) -> bool where T: PartialEq
=> or (Option<T>) -> Option<T>
=> flatten () -> Iterator<U> where T: IntoIterator<U>
=> flat_map ((T) -> IntoIterator<Item = U>) -> Iterator<Item = U>
=> starts_with (&[T]) -> bool where T: PartialEq &str
=> or_else (() -> Option<T>) -> Option<T> => ends_with (&[T]) -> bool where T: PartialEq
=> xor (Option<T>) -> Option<T> // Chars
// Taking and skipping
// Binary searching => chars () -> Iterator<Item = char>
=> skip (usize) -> Iterator<Item = T>
=> binary_search (&T) -> Result<usize, usize> where T: Ord
Option<&T> => take (usize) -> Iterator<Item = T> => binary_search_by ((&T) -> Ordering) -> Result<usize, usize>
=> char_indices () -> Iterator<Item = (usize, char)>
=> is_char_boundary (usize) -> bool
=> skip_while ((&T) -> bool) -> Iterator<Item = T>
=> binary_search_by_key (&B, (&T) -> B) -> Result<usize, usize> where B: Ord
// Cloning inner => take_while ((&T) -> bool) -> Iterator<Item = T>
// Bytes
=> cloned () -> Option<T> where T: Clone => step_by (usize) -> Iterator<Item = T> // Getting and iterating
=> bytes () -> Iterator<Item = u8>
=> copied () -> Option<T> where T: Copy => first () -> Option<&T>
=> as_bytes () -> &[u8]
// Misc. iterating
=> last () -> Option<&T>
=> for_each ((T) -> ()) -> ()
Option<Option<T>> => inspect ((&T) -> ()) -> Iterator<Item = T>
=> get (SliceIndex<[T]>) -> Option<&T>
=> iter () -> Iterator<Item = &T>
// Splitting to two parts
=> split_at (usize) -> (&str, &str)
=> scan (S, (&mut S, T) -> Option<U>) -> Iterator<Item = U>
=> flatten () -> Option<T>
// Length // Splitting to iterator
// Calculations
=> len () -> usize => lines () -> Iterator<Item = &str>
Option<Result<T, E>> => sum () -> S where S: Sum<T>
=> product () -> P where P: Product<T>
=> is_empty () -> bool => split_whitespace () -> Iterator<Item = &str>
=> split_ascii_whitespace () -> Iterator<Item = &str>
=> transpose () -> Result<Option<T>, E>
// Maximum and minimum &mut [T] =>
=>
split (Pattern) -> Iterator<Item = &str>
rsplit (Pattern) -> Iterator<Item = &str>
=> max () -> Option<T> where T: Ord
&Option<T> => min () -> Option<T> where T: Ord // Splitting to iterator
=> splitn (usize, Pattern) -> Iterator<Item = &str>

Dark Single Large GitHub


// Checking inner => max_by ((&T, &T) -> Ordering) -> Option<T> => split_mut ((&T) -> bool) -> Iterator<Item = &mut [T]> => rsplitn (usize, Pattern) -> Iterator<Item = &str>
=> is_some () -> bool => min_by ((&T, &T) -> Ordering) -> Option<T> => rsplit_mut ((&T) -> bool) -> Iterator<Item = &mut [T]> => split_terminator (Pattern) -> Iterator<Item = &str>
=> is_none () -> bool => max_by_key ((&T) -> U) -> Option<T> where U: Ord => splitn_mut (usize, (&T) -> bool) -> Iterator<Item = &mut [T]> => rsplit_terminator (Pattern) -> Iterator<Item = &str>
=> min_by_key ((&T) -> U) -> Option<T> where U: Ord => rsplitn_mut (usize, (&T) -> bool) -> Iterator<Item = &mut [T]>
// To inner reference // Trimming
=> as_ref () -> Option<&T> // Comparing with another iterator // Splitting at position => trim () -> &str
=> iter () -> Iterator<&T> => eq (IntoIterator<Item = T>) -> bool where T: PartialEq => split_at_mut (usize) -> (&mut [T], &mut [T]) => trim_start () -> &str
=> as_deref () -> Option<&U> => ne (IntoIterator<Item = T>) -> bool where T: PartialEq => split_first_mut () -> Option<(&mut T, &mut [T])> => trim_end () -> &str
where T: Deref<Target = U> => lt (IntoIterator<Item = T>) -> bool where T: PartialOrd => split_last_mut () -> Option<(&mut T, &mut [T])> => trim_matches (Pattern) -> &str
=> le (IntoIterator<Item = T>) -> bool where T: PartialOrd => trim_start_matches (Pattern) -> &str
// Chunks
&mut Option<T> => gt (IntoIterator<Item = T>) -> bool where
=> ge (IntoIterator<Item = T>) -> bool where T: PartialOrd
T: PartialOrd
=> chunks_mut (usize) -> Iterator<Item = &mut [T]>
=> trim_end_matches (Pattern) -> &str

=> chunks_exact_mut (usize) -> Iterator<Item = &mut [T]> // Matching and finding
=> cmp (IntoIterator<Item = T>) -> Ordering where T: Ord
// To inner mutable reference
=> rchunks_mut (usize) -> Iterator<Item = &mut [T]> => contains (Pattern) -> bool
=> partial_cmp (IntoIterator<Item = T>)
=> as_mut () -> Option<&mut T> => rchunks_exact_mut (usize) -> Iterator<Item = &mut [T]> => starts_with (Pattern) -> bool
-> Option<Ordering> where T: PartialOrd
=> iter_mut () -> Iterator<&mut T>
=> ends_with (Pattern) -> bool
=> as_deref_mut () -> Option<&mut U> // Sorting
// Reversing and cycling => find (Pattern) -> Option<usize>
where T: DerefMut + Deref<Target = U> => sort () where T: Ord
=> rev () -> Iterator<Item = T> where Self: DoubleEndedIterator => rfind (Pattern) -> Option<usize>
=> sort_by ((&T, &T) -> Ordering)
=> cycle () -> Iterator<Item = T> where Self: Clone => matches (Pattern) -> Iterator<Item = &str>
// Mutation
=> sort_by_key ((&T) -> K) where K: Ord
=> take () -> Option<T> => rmatches (Pattern) -> Iterator<Item = &str>
=> sort_by_cached_key ((&T) -> K) where K: Ord
=> replace (T) -> Option<T> Iterator<Item = &T> => sort_unstable () where T: Ord
=> match_indices (Pattern) -> Iterator<Item = (usize, &str)>
=> rmatch_indices (Pattern) -> Iterator<Item = (usize, &str)>
=> insert (T) -> &mut T
=> sort_unstable_by ((&T, &T) -> Ordering)
=> get_or_insert (T) -> &mut T // Cloning inner
=> sort_unstable_by_key ((&T) -> K) where K: Ord // Case
=> get_or_insert_with (() -> T) -> &mut T => cloned () -> Iterator<T> where T: Clone
=> to_uppercase () -> String
=> copied () -> Iterator<T> where T: Copy // Rearranging
=> to_lowercase () -> String
Result<T, E> => swap (usize, usize)
=> to_ascii_uppercase () -> String
&mut Iterator<Item = T> => reverse ()
=> rotate_left (usize)
=> to_ascii_lowercase () -> String
// To inner type => eq_ignore_ascii_case (&str) -> bool
=> rotate_right (usize)
=> unwrap () -> T where E: Debug // Finding and positioning
// Replacing
=> unwrap_err () -> E where T: Debug => find ((&T) -> bool) -> Option<T>
// Overriding
=> replace (Pattern, &str) -> String
=> unwrap_or (T) -> T => find_map (( T) -> Option<U>) -> Option<U>
=> swap_with_slice (&mut [T]) => replacen (Pattern, &str, usize) -> String
=> unwrap_or_else ((E) -> T) -> T => position (( T) -> bool) -> Option<usize>
=> copy_from_slice (&[T]) where T: Copy
=> unwrap_or_default () -> T where T: Default => rposition (( T) -> bool) -> Option<usize> => clone_from_slice (&[T]) where T: Clone // Length
=> expect (&str) -> T where Self: ExactSizeIterator + DoubleEndedIterator
=> len () -> usize
=> expect_err (&str) -> E // Getting and iterating
=> is_empty () -> bool
// Boolean operations
=> ok () -> Option<T> => first_mut () -> Option<&mut T>
=> all ((T) -> bool) -> bool
=> err () -> Option<E> => last_mut () -> Option<&mut T> // Misc.
=> any ((T) -> bool) -> bool
=> get_mut (SliceIndex<[T]>) -> Option<&mut T> => is_ascii () -> bool
// Mapping
=> iter_mut () -> Iterator<Item = &mut T> => repeat (usize) -> String
// Try iterating
=> map ((T) -> U) -> Result<U, E>
=> encode_utf16 () -> Iterator<Item = u16>
=> try_for_each ((T) -> R) -> R where R: Try<Ok = ()>
=> map_err ((E) -> F) -> Result<T, F>
=> map_or (U, (T) -> U) -> U
=> try_fold (S, (S, T) -> R) -> R where R: Try<Ok = S> &mut Vec<T> => parse () -> Result<F, F::Err> where F: FromStr

=> map_or_else ((E) -> U, (T) -> U) -> U


// Adding and removing single item

Dark Single Large GitHub


//
=>
Conditioning
and (Result<U, E>) -> Result<U, E>
iter =>
=>
push (T)
pop () -> Option<T>
&mut str
=> and_then ((T) -> Result<U, E>) -> Result<U, E> // Creating simple iterators => insert (usize, T) // Splitting to two parts
=> or (Result<T, F>) -> Result<T, F> => remove (usize) -> T
:: empty () -> Iterator<Item = T> => split_at_mut (usize) -> (&mut str, &mut str)
=> or_else ((E) -> Result<T, F>) -> Result<T, F> => swap_remove (usize) -> T
:: once (T) -> Iterator<Item = T>
// Case conversion
:: once_with (() -> T) -> Iterator<Item = T>
// Extending
Result<Option<T>, E> :: repeat (T) -> Iterator<Item = T> where T: Clone
=> append (&mut Vec<T>)
=> make_ascii_uppercase ()
=> make_ascii_lowercase ()
:: repeat_with (() -> T) -> Iterator<Item = T> => extend (IntoIterator<Item = T>)
// Transposing :: from_fn (() -> Option<T>) -> Iterator<Item = T>
=> extend (IntoIterator<Item = &T>) where T: Copy
=> transpose () -> Option<Result<T, E>> :: successors (Option<T>, (&T) -> Option<T>) -> Iterator<Item = T> => extend_from_slice (&[T]) where T: Clone &mut String
&Result<T, E> // Resizing
=> truncate (usize)
// Inserting and appending string
=> push_str (&str)
=> resize (usize, T) where T: Clone => insert_str (usize, &str)
// Checking inner
=> resize_with (usize, () -> T)
=> is_ok () -> bool // Adding and removing char
=> is_err () -> bool
// Clearing => push (char)
=> clear () => pop () -> Option<char>
// To inner reference
=> retain ((&T) -> bool) => insert (usize, char)
=> as_ref () -> Result<&T, &E>
=> remove (usize) -> char
=> iter () -> Iterator<Item = &T>
// Removing or replacing range into iterator
=> drain (RangeBounds<usize>) -> Iterator<T> // Clearing
&mut Result<T, E> => splice (RangeBounds<usize>, IntoIterator<Item = T>) -> Iterator<T> => clear ()
=> truncate (usize)
// Deduplicating
// To inner mutable reference => retain ((char) -> bool)
=> dedup () where T: PartialEq
=> as_mut () -> Result<&mut T, &mut E>
=> dedup_by ((&mut T, &mut T) -> bool) // Capacity manipulation
=> iter_mut () -> Iterator<Item = &mut T>
=> dedup_by_key ((&mut T) -> K) where K: PartialEq => reserve (usize)
=> reserve_exact (usize)
// Splitting off
=> shrink_to_fit ()
=> split_off (usize) -> Vec<T>
// Misc.
// Capacity manipulation
=> split_off (usize) -> String
=> reserve (usize)
=> replace_range (RangeBounds<usize>, &str)
=> reserve_exact (usize)
=> drain (RangeBounds<usize>) -> Iterator<Item = char>
=> shrink_to_fit ()

slice
// Creating slice from reference
:: from_ref (&T) -> &[T]

Dark Single Large GitHub

You might also like