本文簡要介紹rust語言中 std::string::String.reserve_exact
的用法。
用法
pub fn reserve_exact(&mut self, additional: usize)
確保此 String
的容量大於其長度 additional
字節。
考慮使用 reserve
方法,除非您絕對比分配器更了解。
Panics
如果新容量溢出 usize
會出現Panics。
例子
基本用法:
let mut s = String::new();
s.reserve_exact(10);
assert!(s.capacity() >= 10);
這實際上可能不會增加容量:
let mut s = String::with_capacity(10);
s.push('a');
s.push('b');
// s now has a length of 2 and a capacity of 10
assert_eq!(2, s.len());
assert_eq!(10, s.capacity());
// Since we already have an extra 8 capacity, calling this...
s.reserve_exact(8);
// ... doesn't actually increase.
assert_eq!(10, s.capacity());
相關用法
- Rust String.reserve用法及代碼示例
- Rust String.remove用法及代碼示例
- Rust String.replace_range用法及代碼示例
- Rust String.remove_matches用法及代碼示例
- Rust String.retain用法及代碼示例
- Rust String.try_reserve用法及代碼示例
- Rust String.insert_str用法及代碼示例
- Rust String.into_raw_parts用法及代碼示例
- Rust String.extend_from_within用法及代碼示例
- Rust String.clear用法及代碼示例
- Rust String.from_utf16_lossy用法及代碼示例
- Rust String.push用法及代碼示例
- Rust String.as_bytes用法及代碼示例
- Rust String.as_str用法及代碼示例
- Rust String.with_capacity用法及代碼示例
- Rust String.from_utf8_lossy用法及代碼示例
- Rust String.into_bytes用法及代碼示例
- 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-lang.org大神的英文原創作品 std::string::String.reserve_exact。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。