将 rust 标准库中的几个常用类型的方法安装第一个入参(即:Move、借用、可变借用)分组整理,以便开发时做个参考。
Type | (..) | (self..) | (&self..) | (&mut self..) |
---|
Cow | from() | into_owned() | | to_mut() |
Cell | from()
from_mut()
new() | into_inner() | set()
get()
take()
replace() | get_mut() |
RefCell | from()
new() | into_inner() | borrow()
borrow_mut()
take()
replace() | get_mut() |
Vec | from()
new()
with_capacity() | into_boxed_slice() | len()
capacity()
is_empyt() | insert()
append()
push()
pop()
remove()
drain()
clear()
splice()
reserve()
resize()
shrink_to()
truncate() |
BTreeMap | from()
new() | into_keys()
range() | get()
len()
keys()
values()
iter()
get_key_value()
first_key_value()
last_key_value()
contains_key()
is_empyt() | insert()
append()
extend
retain()
remove()
pop_first()
pop_last()
entry()
first_entry()
last_entry() |
HashMap | from()
new() | keys()
into_keys()
capacity() | get()
len()
values()
iter()
is_empty() | insert()
extend
retain()
drain()
remove()
remove_entry()
reserve()
entry() |
Box | from()
new()
leak()
pin() | downcast()
split() | borrow() | borrow_mut()
consume()
read_line() |
同时还能发现一些规律,比如:
- 自动解引用的类型还是少数,本文只有 Vec,但手动解引用的基本都会 impl。
- 同名的方法要注意,比如 Vec 的 len() 和自动解引用后数组的 len()。
- 一般都会实现个性化的 Clone、Debug、Defaut、Dref、Drop、From 等 Trait,而 Borrow、Into、ToOwned 等使用 Blanket Trait 实现。
Blanket Implementations(一揽子实现) 即 Trait 的默认实现,几乎每个类型都实现了下面的“一揽子实现”:
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T> ToString for T | | | to_string() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
string
string::String
borrow
borrow::Cow
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<B> Cow<'_, B> | | into_owned() | | to_mut() |
Trait Implementations | | | | |
impl<B> Clone for Cow<'_, B> | | | clone() | clone_from() |
impl<B> Deref for Cow<'_, B> | | | deref() | |
impl<'a> Extend<Cow<'a, str>> for String | | | | extend() |
impl From<XXX> for Cow | from(XXX) | | | |
impl From<Cow<XXX> for YYY | from(Cow<XXX>) | | | |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T> ToString for T | | | to_string() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
cell
突破“一个数据只有一个所有者、不可变不允许做可变借用、可变与不可变借用不能同时存在 —— 三大编译器限制”,手工解决共享访问及数据竞争问题。
cell::Cell
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<T> Cell<T> | new(T) | into_inner() | set() | |
| | | swap() | |
| | | replace() | |
| | | as_slice_of_cells() | |
impl<T> Cell<T>
where T: Copy | | | get() | |
| | | update() | |
impl<T> Cell<T>
where T: ?Sized | from_mut() | | as_ptr() | get_mut() |
impl<T> Cell<T>
where T: Default | | | take() | |
Trait Implementations | | | | |
impl<T> Clone for Cell<T>
where T: Copy | | | clone() | clone_from() |
impl<T> From<T> for Cell<T> | from(T) | | | |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
cell::RefCell
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<T> RefCell<T> | new(T) | into_inner() | replace() | |
| | | replace_with() | |
| | | swap() | |
impl<T> RefCell<T>
where T: ?Sized | | | borrow() | get_mut() |
| | | try_borrow() | undo_leak() |
| | | borrow_mut() | |
| | | try_borrow_mut() | |
impl<T> RefCell<T>
where T: Default | | | take() | |
Trait Implementations | | | | |
impl<T> Clone for RefCell<T>
where T: Clone | | | clone() | clone_from() |
impl<T> From<T> for Cell<T> | from(T) | | | |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
vec
vec::Vec
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<T> Vec<T, Global> | new() | | | |
| with_capacity() | | | |
impl<T, A> Vec<T, A> | new_in(A) | | capacity() | reserve() |
| | | | try_reserve() |
| | | | shrink_to_fit() |
| | | | shrink_to() |
| | | | truncate() |
| | into_boxed_slice() | as_slice() | as_mut_slice() |
| | | as_ptr() | as_mut_ptr() |
| | | len() | insert() |
| | | is_empty() | remove() |
| | | | retain() |
| | | | push() |
| | | | pop() |
| | | | append() |
| | | | drain() |
| | | | clear() |
| | | | split_off() |
impl<T, A> Vec<T, A>
where T:Clone | | | | resize() |
impl<T, A> Vec<T, A>
where A:Allocator | | | | splice() |
Deref<Target = [T]> 自动解引用 | | | ** len()** | |
| (略) | (略) | (略) | (略) |
Trait Implementations | | | | |
impl<T, A> AsMut<[T]> for Vec<T, A> | | | | as_mut() |
impl<T, A> AsRef<[T]> for Vec<T, A> | | | as_ref() | |
impl<T, A> Clone for Vec<T, A> | | | clone() | clone_from() |
impl<T, A> Deref for Vec<T, A> | | | deref() 手工解引用 | |
impl<T, A> DerefMut for Vec<T, A> | | | | deref_mut() |
impl<T, A> Drop for Vec<T, A> | | | | drop() |
impl<T> From<XXX> for Vec<T, Global> | from(XXX) | | | |
impl<T> From<Vec<XXX>> for YYY | from(<Vec<XXX>>) | | | |
impl<T, A> Hash for Vec<T, A> | | hash() | | |
| | hash_slice() | | |
impl<'a, T, A> Extend<&'a T> for Vec<T, A> | | | | extend() |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
collections
collections::BTreeMap
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<K, V> BTreeMap<K, V, Global> | new() | | | |
impl<K, V, A> BTreeMap<K, V, A> | | into_keys() | get() | clear() |
| | | get_key_value() | pop_first() |
| | | first_key_value() | first_entry() |
| | | last_key_value() | last_entry() |
| | | contains_key() | pop_last() |
| | | len() | get_mut() |
| | | is_empyt() | insert() |
| | | | remove() |
| | | | retain() |
| | | | append() |
| | range() | | range_mut() |
| | | keys() | entry() |
| | | | split_off() |
| | | iter() | iter_mut() |
| | | values() | values_mut() |
Trait Implementations | | | | |
impl<K, V, A> Clone for BTreeMap<K, V, A> | | | clone() | |
impl<K, V, A> Extend<(K, V)> for BTreeMap<K, V, A> | | | | extend() |
| | | | extend_one() |
| | | | extend_reserve() |
impl ... From<..> for HashMap<..> | from(arr: [(K, V); N]) | | | |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
collections::HashMap
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<K, V> HashMap<K, V, RandomState> | new() | | | |
impl<K, V, S> HashMap<K, V, S> | | capacity() | | |
| | keys() | values() | values_mut() |
| | into_keys() | is_empty() | drain() |
| | into_values() | len() | drain_filter() |
| | | iter() | iter_mut() |
| | | | retain() |
| | | hasher() | clear() |
impl<K, V, S> HashMap<K, V, S> | | | | reserve() |
| | | get_key_value() | shrink_to_fit() |
| | | contains_key() | shrink_to() |
| | | | entry() |
| | | get() | get_mut() |
| | | | insert() |
| | | | remove() |
| | | | remove_entry() |
Trait Implementations | | | | |
impl<K, V, A> Clone for HashMap<K, V, A> | | | clone() | |
impl<K, V, A> Extend<(K, V)> for HashMap<K, V, A> | | | | extend() |
| | | | extend_one() |
| | | | extend_reserve() |
impl ... From<..> for HashMap<..> | from(arr: [(K, V); N]) | | | |
| | | | |
| | | | |
| | | | |
| | | | |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T, U> TryFrom<U> for T | try_from() | | | |
boxed
boxed::Box
impl | (..) | (self..) | (&self..) | (&mut self..) |
---|
Methods | | | | |
impl<T> Box<T, Global> | new(T) | | | |
| pin(T) | | | |
impl<T> Box<T, Global> | from_raw(T) | | | |
| into_raw() | | | |
| leak() | | | |
impl<A> Box<dyn Any + 'static, A> | | downcast() | | |
Trait Implementations | | | | |
impl<T, A> Borrow<T> for Box<T, A> | | | borrow() | |
impl<T, A> BorrowMut<T> for Box<T, A> | | | | borrow_mut() |
impl<B: BufRead + ?Sized> BufRead for Box<B> | | split() | | fill_buf() |
| | line() | | consume() |
| | | | read_until() |
| | | | read_line() |
impl ... From<XXX> for Box<..> | from(XXX) | | | |
impl ... From<Box<XXX>> for xxx | from(Box<XXX>) | | | |
Auto Trait Implementations | | | | |
Blanket Implementations | | | | |
impl<T> Any for T | | | type_id() | |
impl<T> Borrow<T> for T | | | borrow() | |
impl<T> BorrowMut<T> for T | | | | borrow_mut() |
impl<T> From<T> for T | from() | | | |
impl<T, U> Into<U> for T | | into() | | |
impl<F> IntoFuture for F | | into_future() | | |
impl<T> ToOwned for T | | | to_owned() | |
impl<T> ToString for T | | to_string() | | |
impl<T, U> TryFrom<U> for T | try_from() | | | |