本文簡要介紹rust語言中 alloc::string::String.drain
的用法。
用法
pub fn drain<R>(&mut self, range: R) -> Drain<'_> where R: RangeBounds<usize>,
創建一個排水迭代器,該迭代器刪除 String
中的指定範圍並產生已刪除的 chars
。
注意:即使迭代器直到最後才被消耗,元素範圍也會被刪除。
Panics
如果起點或終點不在 char
邊界上,或者它們超出邊界,則會出現Panics。
例子
基本用法:
let mut s = String::from("α is alpha, β is beta");
let beta_offset = s.find('β').unwrap_or(s.len());
// Remove the range up until the β from the string
let t: String = s.drain(..beta_offset).collect();
assert_eq!(t, "α is alpha, ");
assert_eq!(s, "β is beta");
// A full range clears the string
s.drain(..);
assert_eq!(s, "");
相關用法
- Rust String.drain用法及代碼示例
- Rust String.try_reserve用法及代碼示例
- Rust String.insert_str用法及代碼示例
- Rust String.into_raw_parts用法及代碼示例
- Rust String.extend_from_within用法及代碼示例
- Rust String.clear用法及代碼示例
- Rust String.reserve用法及代碼示例
- Rust String.from_utf16_lossy用法及代碼示例
- Rust String.push用法及代碼示例
- Rust String.as_bytes用法及代碼示例
- Rust String.as_str用法及代碼示例
- Rust String.with_capacity用法及代碼示例
- Rust String.remove用法及代碼示例
- Rust String.from_utf8_lossy用法及代碼示例
- Rust String.into_bytes用法及代碼示例
- Rust String.replace_range用法及代碼示例
- Rust String.split_off用法及代碼示例
- Rust String.from_utf16用法及代碼示例
- Rust String.shrink_to_fit用法及代碼示例
- Rust String.from_utf8_unchecked用法及代碼示例
- Rust String.into_boxed_str用法及代碼示例
- Rust String.new用法及代碼示例
- Rust String.insert用法及代碼示例
- Rust String.capacity用法及代碼示例
- Rust String.try_reserve_exact用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 alloc::string::String.drain。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。