【Rust】字符串(二)
环境
- Time 2022-03-24
- Rust 1.59.0
示例
capacity
fn main() {
let name = String::from("JiangBo");
println!("{:?}", name.capacity());
}
reserve
fn main() {
let mut name = String::from("JiangBo");
name.reserve(10);
println!("{:?}", name.capacity());
}
reserve_exact
fn main() {
let mut name = String::from("JiangBo");
name.reserve_exact(10);
println!("{:?}", name.capacity());
}
shrink_to_fit
fn main() {
let mut name = String::from("JiangBo");
name.reserve_exact(10);
println!("{:?}", name.capacity());
name.shrink_to_fit();
println!("{:?}", name.capacity());
}
shrink_to
fn main() {
let mut name = String::from("JiangBo");
name.reserve_exact(10);
println!("{:?}", name.capacity());
name.shrink_to(10);
println!("{:?}", name.capacity());
}
push
fn main() {
let mut name = String::from("JiangBo");
name.push('!');
println!("{name:?}");
}
as_bytes
fn main() {
let name = String::from("JiangBo");
let name = name.as_bytes();
println!("{name:?}");
}
truncate
fn main() {
let mut name = String::from("JiangBo");
name.truncate(5);
println!("{name:?}");
}
pop
fn main() {
let mut name = String::from("JiangBo");
name.pop();
println!("{name:?}");
}
总结
了解了字符串中相关的一些方法。