当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust String.shrink_to_fit用法及代码示例


本文简要介绍rust语言中 alloc::string::String.shrink_to_fit 的用法。

用法

pub fn shrink_to_fit(&mut self)

缩小此 String 的容量以匹配其长度。

例子

基本用法:

let mut s = String::from("foo");

s.reserve(100);
assert!(s.capacity() >= 100);

s.shrink_to_fit();
assert_eq!(3, s.capacity());

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 alloc::string::String.shrink_to_fit。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。