Rust语法总结

字号+ 编辑: 国内TP粉 修订: 呆头鹅甲 来源: github 2018-04-14 22:10 我要说两句(0)

牛人总结。

Option<T>

To inner type

fn unwrap () -> T
fn unwrap_or (T) -> T
fn unwrap_or_else (() -> T) -> T
fn unwrap_or_default () -> T where T: Default
fn expect (&str) -> T

Converting to another type

fn map ((T) -> U) -> Option<U>
fn map_or (U, (T) -> U) -> U
fn map_or_else (() -> U, (T) -> U) -> U

To Result

fn ok_or (E) -> Result<T, E>
fn ok_or_else (() -> E) -> Result<T, E>

Conditioning

fn filter ((&T) -> bool) -> Option<T>
fn and (Option<U>) -> Option<U>
fn and_then ((T) -> Option<U>) -> Option<U>
fn or (Option<T>) -> Option<T>
fn or_else (() -> Option<T>) -> Option<T>
fn xor (Option<T>) -> Option<T>

Option<&T>

Cloning inner

fn cloned () -> Option<T> where T: Clone
fn copied () -> Option<T> where T: Copy

Option<Option<T>>

fn flatten () -> Option<T>

Option<Result<T, E>>

fn transpose () -> Result<Option<T>, E>

&Option<T>

Checking inner

fn is_some () -> bool
fn is_none () -> bool

To inner reference

fn as_ref () -> Option<&T>
fn iter () -> Iterator<&T>
fn as_deref () -> Option<&U>where T: Deref<Target = U>

&mut Option<T>

To inner mutable reference

fn as_mut () -> Option<&mut T>
fn iter_mut () -> Iterator<&mut T>
fn as_deref_mut () -> Option<&mut U>where T: DerefMut + Deref<Target = U>

Mutation

fn take () -> Option<T>
fn replace (T) -> Option<T>
fn get_or_insert (T) -> &mut T
fn get_or_insert_with (() -> T) -> &mut T

Result<T, E>

To inner type

fn unwrap () -> T where E: Debug
fn unwrap_err () -> E where T: Debug
fn unwrap_or (T) -> T
fn unwrap_or_else ((E) -> T) -> T
fn unwrap_or_default () -> T where T: Default
fn expect (&str) -> T
fn expect_err (&str) -> E
fn ok () -> Option<T>
fn err () -> Option<E>

Mapping

fn map ((T) -> U) -> Result<U, E>
fn map_err ((E) -> F) -> Result<T, F>
fn map_or (U, (T) -> U) -> U
fn map_or_else ((E) -> U, (T) -> U) -> U

Conditioning

fn and (Result<U, E>) -> Result<U, E>
fn and_then ((T) -> Result<U, E>) -> Result<U, E>
fn or (Result<T, F>) -> Result<T, F>
fn or_else ((E) -> Result<T, F>) -> Result<T, F>

Result<Option<T>, E>

Transposing

fn transpose () -> Option<Result<T, E>>

&Result<T, E>

Checking inner

fn is_ok () -> bool
fn is_err () -> bool

To inner reference

fn as_ref () -> Result<&T, &E>
fn iter () -> Iterator<Item = &T>

&mut Result<T, E>

To inner mutable reference

fn as_mut () -> Result<&mut T, &mut E>
fn iter_mut () -> Iterator<Item = &mut T>

Iterator<Item = T>

Mapping and filtering

fn map        (( T) -> U)         -> Iterator<Item = U>
fn filter     ((&T) -> bool)      -> Iterator<Item = T>
fn filter_map (( T) -> Option<U>) -> Iterator<Item = U>

Collecting and folding

fn fold (S, (S, T) -> S) -> S
fn collect () -> B where B: FromIterator<T>
fn partition ((&T) -> bool) -> (B, B) where B: Default + Extend<T>

Counting and enumerating

fn count () -> usize
fn last () -> Option<T>
fn enumerate () -> Iterator<Item = (usize, T)>

Combining with other iterators

fn zip   (IntoIterator<Item = U>) -> Iterator<Item = (T, U)>
fn chain (IntoIterator<Item = T>) -> Iterator<Item = T>

Flattening

fn flatten () -> Iterator<U> where T: IntoIterator<U>
fn flat_map ((T) -> IntoIterator<Item = U>) -> Iterator<Item = U>

Taking and skipping

fn skip (usize) -> Iterator<Item = T>
fn take (usize) -> Iterator<Item = T>
fn skip_while ((&T) -> bool) -> Iterator<Item = T>
fn take_while ((&T) -> bool) -> Iterator<Item = T>
fn step_by (usize) -> Iterator<Item = T>

Misc. iterating

fn for_each ((T) -> ()) -> ()
fn inspect ((&T) -> ()) -> Iterator<Item = T>
fn scan (S, (&mut S, T) -> Option<U>) -> Iterator<Item = U>

Calculations

fn sum     () -> S where S: Sum<T>
fn product () -> P where P: Product<T>

Maximum and minimum

fn max () -> Option<T> where T: Ord
fn min () -> Option<T> where T: Ord
fn max_by ((&T, &T) -> Ordering) -> Option<T>
fn min_by ((&T, &T) -> Ordering) -> Option<T>
fn max_by_key ((&T) -> U) -> Option<T> where U: Ord
fn min_by_key ((&T) -> U) -> Option<T> where U: Ord

Comparing with another iterator

fn eq (IntoIterator<Item = T>) -> bool where T: PartialEq
fn ne (IntoIterator<Item = T>) -> bool where T: PartialEq
fn lt (IntoIterator<Item = T>) -> bool where T: PartialOrd
fn le (IntoIterator<Item = T>) -> bool where T: PartialOrd
fn gt (IntoIterator<Item = T>) -> bool where T: PartialOrd
fn ge (IntoIterator<Item = T>) -> bool where T: PartialOrd
fn cmp (IntoIterator<Item = T>) -> Ordering where T: Ord
fn partial_cmp (IntoIterator<Item = T>)
-> Option<Ordering> where T: PartialOrd

Reversing and cycling

fn rev   () -> Iterator<Item = T> where Self: DoubleEndedIterator
fn cycle () -> Iterator<Item = T> where Self: Clone

Iterator<Item = &T>

Cloning inner

fn cloned () -> Iterator<T> where T: Clone
fn copied () -> Iterator<T> where T: Copy

&mut Iterator<Item = T>

Finding and positioning

fn find      ((&T) -> bool)      -> Option<T>

fn find_map  (( T) -> Option<U>) -> Option<U>

fn position  (( T) -> bool)      -> Option<usize>

fn rposition (( T) -> bool)      -> Option<usize>where Self: ExactSizeIterator + DoubleEndedIterator

Boolean operations

fn all ((T) -> bool) -> bool

fn any ((T) -> bool) -> bool

Try iterating

fn try_for_each   ((T) -> R) -> R where R: Try<Ok = ()>

fn try_fold (S, (S, T) -> R) -> R where R: Try<Ok = S>

iter

Creating simple iterators

fn empty () -> Iterator<Item = T>

fn once (T) -> Iterator<Item = T>

fn once_with (() -> T) -> Iterator<Item = T>

fn repeat (T) -> Iterator<Item = T> where T: Clone

fn repeat_with (() -> T) -> Iterator<Item = T>

fn from_fn (() -> Option<T>) -> Iterator<Item = T>

fn successors (Option<T>, (&T) -> Option<T>) -> Iterator<Item = T>

&[T]

Splitting to iterator

fn split  ((&T) -> bool) -> Iterator<Item = &[T]>

fn rsplit ((&T) -> bool) -> Iterator<Item = &[T]>

fn splitn  (usize, (&T) -> bool) -> Iterator<Item = &[T]>

fn rsplitn (usize, (&T) -> bool) -> Iterator<Item = &[T]>

Splitting at position

fn split_at (usize) -> (&[T], &[T])

fn split_first () -> Option<(&T, &[T])>

fn split_last  () -> Option<(&T, &[T])>

Chunks and windows

fn chunks        (usize) -> Iterator<Item = &[T]>

fn chunks_exact  (usize) -> Iterator<Item = &[T]>

fn rchunks       (usize) -> Iterator<Item = &[T]>

fn rchunks_exact (usize) -> Iterator<Item = &[T]>

fn windows       (usize) -> Iterator<Item = &[T]>

Matching

fn contains    (&T)   -> bool where T: PartialEq

fn starts_with (&[T]) -> bool where T: PartialEq

fn ends_with   (&[T]) -> bool where T: PartialEq

Binary searching

fn binary_search (&T)                   -> Result<usize, usize> where T: Ord

fn binary_search_by ((&T) -> Ordering)  -> Result<usize, usize>

fn binary_search_by_key (&B, (&T) -> B) -> Result<usize, usize> where B: Ord

Getting and iterating

fn first () -> Option<&T>

fn last  () -> Option<&T>

fn get (SliceIndex<[T]>

) -> Option<&T>

fn iter () -> Iterator<Item = &T>

Length

fn len () -> usize

fn is_empty () -> bool

&mut [T] https://doc.rust-lang.org/std/primitive.slice.html

Splitting to iterator

fn split_mut  ((&T) -> bool) -> Iterator<Item = &mut [T]>

fn rsplit_mut ((&T) -> bool) -> Iterator<Item = &mut [T]>

fn splitn_mut  (usize, (&T) -> bool) -> Iterator<Item = &mut [T]>

fn rsplitn_mut (usize, (&T) -> bool) -> Iterator<Item = &mut [T]>

Splitting at position

fn split_at_mut (usize) -> (&mut [T], &mut [T])

fn split_first_mut () -> Option<(&mut T, &mut [T])>

fn split_last_mut  () -> Option<(&mut T, &mut [T])>

Chunks

fn chunks_mut        (usize) -> Iterator<Item = &mut [T]>

fn chunks_exact_mut  (usize) -> Iterator<Item = &mut [T]>

fn rchunks_mut       (usize) -> Iterator<Item = &mut [T]>

fn rchunks_exact_mut (usize) -> Iterator<Item = &mut [T]>

Sorting

fn sort () where T: Ord

fn sort_by ((&T, &T) -> Ordering)

fn sort_by_key ((&T) -> K) where K: Ord

fn sort_by_cached_key ((&T) -> K) where K: Ord

fn sort_unstable () where T: Ord

fn sort_unstable_by ((&T, &T) -> Ordering)

fn sort_unstable_by_key ((&T) -> K) where K: Ord

Rearranging

fn swap (usize, usize)

fn reverse ()

fn rotate_left (usize)

fn rotate_right (usize)

Overriding

fn swap_with_slice  (&mut [T])

fn copy_from_slice  (&[T]) where T: Copy

fn clone_from_slice (&[T]) where T: Clone

Getting and iterating

fn first_mut () -> Option<&mut T>

fn last_mut  () -> Option<&mut T>

fn get_mut (SliceIndex<[T]>

) -> Option<&mut T>

fn iter_mut () -> Iterator<Item = &mut T>

&mut Vec<T>

Adding and removing single item

fn push (T)

fn pop () -> Option<T>

fn insert (usize, T)

fn remove (usize) -> T

fn swap_remove (usize) -> T

Extending

fn append (&mut Vec<T>)

fn extend (IntoIterator<Item = T>)

fn extend (IntoIterator<Item = &T>) where T: Copy

fn extend_from_slice (&[T]) where T: Clone

Resizing

fn truncate (usize)

fn resize (usize, T) where T: Clone

fn resize_with (usize, () -> T)

Clearing

fn clear ()

fn retain ((&T) -> bool)

Removing or replacing range into iterator

fn drain  (RangeBounds<usize>

) -> Iterator<T>

fn splice (RangeBounds<usize>

, IntoIterator<Item = T>) -> Iterator<T>

Deduplicating

fn dedup () where T: PartialEq

fn dedup_by ((&mut T, &mut T) -> bool)

fn dedup_by_key ((&mut T) -> K) where K: PartialEq

Splitting off

fn split_off (usize) -> Vec<T>

Capacity manipulation

fn reserve (usize)
fn reserve_exact (usize)
fn shrink_to_fit ()

slice

Creating slice from reference

fn from_ref (&T) -> &[T]

fn from_mut (&mut T) -> &mut [T]

&[u8]

ASCII

fn is_ascii () -> bool

fn eq_ignore_ascii_case (&[u8]) -> bool

fn to_ascii_uppercase () -> Vec<u8>

fn to_ascii_lowercase () -> Vec<u8>

&mut [u8]

ASCII

fn make_ascii_uppercase ()

fn make_ascii_lowercase ()

str

Bytes

fn from_utf8     (&[u8])     -> Result<&str, Utf8Error>

fn from_utf8_mut (&mut [u8]) -> Result<&mut str, Utf8Error>

&str

Chars

fn chars () -> Iterator<Item = char>

fn char_indices () -> Iterator<Item = (usize, char)>

fn is_char_boundary (usize) -> bool

Bytes

fn bytes () -> Iterator<Item = u8>

fn as_bytes () -> &[u8]

Splitting to two parts

fn split_at (usize) -> (&str, &str)

Splitting to iterator

fn lines () -> Iterator<Item = &str>

fn split_whitespace () -> Iterator<Item = &str>

fn split_ascii_whitespace () -> Iterator<Item = &str>

fn split  (Pattern

) -> Iterator<Item = &str>

fn rsplit (Pattern

) -> Iterator<Item = &str>

fn splitn  (usize, Pattern

) -> Iterator<Item = &str>

fn rsplitn (usize, Pattern

) -> Iterator<Item = &str>

fn split_terminator  (Pattern

) -> Iterator<Item = &str>

fn rsplit_terminator (Pattern

) -> Iterator<Item = &str>

Trimming

fn trim       () -> &str

fn trim_start () -> &str

fn trim_end   () -> &str

fn trim_matches       (Pattern

) -> &str

fn trim_start_matches (Pattern

) -> &str

fn trim_end_matches   (Pattern

) -> &str

Matching and finding

fn contains    (Pattern

) -> bool

fn starts_with (Pattern

) -> bool

fn ends_with   (Pattern

) -> bool

fn find  (Pattern

) -> Option<usize>

fn rfind (Pattern

) -> Option<usize>

fn matches  (Pattern

) -> Iterator<Item = &str>

fn rmatches (Pattern

) -> Iterator<Item = &str>

fn match_indices  (Pattern

) -> Iterator<Item = (usize, &str)>

fn rmatch_indices (Pattern

) -> Iterator<Item = (usize, &str)>

Case

fn to_uppercase () -> String

fn to_lowercase () -> String

fn to_ascii_uppercase () -> String

fn to_ascii_lowercase () -> String

fn eq_ignore_ascii_case (&str) -> bool

Replacing

fn replace  (Pattern

, &str) -> String

fn replacen (Pattern

, &str, usize) -> String

Length

fn len () -> usize
fn is_empty () -> bool

Misc.

fn is_ascii () -> bool

fn repeat (usize) -> String

fn encode_utf16 () -> Iterator<Item = u16>

fn parse () -> Result<F, F::Err> where F: FromStr

&mut str

Splitting to two parts

fn split_at_mut (usize) -> (&mut str, &mut str)

Case conversion

fn make_ascii_uppercase ()

fn make_ascii_lowercase ()

&mut String

Inserting and appending string

fn push_str (&str)

fn insert_str (usize, &str)

Adding and removing char

fn push (char)

fn pop () -> Option<char>

fn insert (usize, char)

fn remove (usize) -> char

Clearing

fn clear ()

fn truncate (usize)

fn retain ((char) -> bool)

Capacity manipulation

fn reserve (usize)
fn reserve_exact (usize)
fn shrink_to_fit ()

Misc.

fn split_off (usize) -> String
fn replace_range (RangeBounds<usize>
, &str)
fn drain (RangeBounds<usize>
) -> Iterator<Item = char>

阅完此文,您的感想如何?
  • 鼓掌

    1

  • 鄙视

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.交流群: PHP+JS聊天群

相关课文
  • Rust拉取库文件时遇到spurious network error

  • rust架设web执行npm init wasm-app www 报错 Error: Cannot find module '../lib/cli.js'

我要说说
网上宾友点评
沙发已空
Rust语法总结