环境
- Time 2022-05-29
- Rust 1.61.0
- Bytes 1.1.0
前言
说明
参考:https://docs.rs/bytes/latest/bytes/struct.BytesMut.html
目标
使用 BytesMut
的方法。
with_capacity
fn main() {
let bytes = BytesMut::with_capacity(44);
println!("{:?}",bytes);
}
new
fn main() {
let bytes = BytesMut::new();
println!("{:?}", bytes);
}
capacity
fn main() {
let bytes = BytesMut::with_capacity(44);
println!("{:?}", bytes.capacity());
}
len
fn main() {
let bytes = BytesMut::from("JiangBo");
println!("{:?}", bytes.len());
}
put
fn main() {
let mut bytes = BytesMut::from("JiangBo");
bytes.put(b"!".as_ref());
println!("{:?}", bytes);
}
freeze
fn main() {
let bytes = BytesMut::from("JiangBo");
println!("{:?}", bytes.freeze());
}
split_off
fn main() {
let mut bytes = BytesMut::from("JiangBo");
println!("{:?}", bytes.split_off(5));
}
split_to
fn main() {
let mut bytes = BytesMut::from("JiangBo");
println!("{:?}", bytes.split_to(5));
}
split
fn main() {
let mut bytes = BytesMut::from("JiangBo");
println!("{:?}", bytes.split());
}
extend_from_slice
fn main() {
let mut bytes = BytesMut::from("Hello ");
bytes.extend_from_slice("JiangBo".as_ref());
println!("{:?}", bytes);
}
reverse
fn main() {
let mut bytes = BytesMut::from("JiangBo");
bytes.reverse();
println!("{:?}", bytes);
}
clear
fn main() {
let mut bytes = BytesMut::from("JiangBo");
bytes.clear();
println!("{:?}", bytes);
}
resize
fn main() {
let mut bytes = BytesMut::from("JiangBo");
bytes.resize(10, b'o');
println!("{:?}", bytes);
}
总结
使用了 BytesMut
中定义的一些方法。