首页 > 其他分享 >0047-Bytes-BytesMut使用

0047-Bytes-BytesMut使用

时间:2022-09-30 20:35:39浏览次数:84  
标签:Bytes bytes 0047 let println BytesMut main fn

环境

  • 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 中定义的一些方法。

附录

标签:Bytes,bytes,0047,let,println,BytesMut,main,fn
From: https://www.cnblogs.com/jiangbo4444/p/16746029.html

相关文章

  • 0048-Bytes-BufMut使用
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://docs.rs/bytes/latest/bytes/trait.BufMut.html目标使用BufMut的方法。remaining_mutfnma......
  • 0043-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/bytes目标Bytes实现迭代器。IntoIter#[derive(Debug)]pubstructInt......
  • Specified key was too long; max key length is 767 bytes错误的原因
    将mysql数据库里某个UNIQUE唯一索引字段从utf8改为utf8mb4时提示1071-Specifiedkeywastoolong;maxkeylengthis767bytes,来看看这个错误的来原因。来几个知识点......
  • 实例-rust-string和bytes转换
    Cargo.toml[package]name="rust-example9"version="0.1.0"edition="2021"#Seemorekeysandtheirdefinitionsathttps://doc.rust-lang.org/cargo/refere......
  • 0042-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/byteshttps://zhuanlan.zhihu.com/p/109977513目标之前阅读的部分,都......
  • TypeError: Object of type 'bytes' is not JSON serializable
    转载自: https://blog.csdn.net/weixin_41951954/article/details/124838931   ......
  • [Bug0047] Access denied for user '-root'@'localhost' (using password: YES)
    1、问题Accessdeniedforuser'-root'@'localhost'(usingpassword:YES)2、场景低级失误,密码错误3、原因密码错误4、解决方案根据网上资料,出现Accessdenied的......
  • 0039-Bytes-bytes源码阅读
    环境Time2022-05-28Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/bytes目标实现bytes.rs中的一部分方法。线程安全实现了两个线程安全......
  • 0040-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/byteshttps://zhuanlan.zhihu.com/p/109977513目标之前阅读的部分,都......
  • 0041-Bytes-bytes源码阅读
    环境Time2022-05-29Rust1.61.0Bytes1.1.0前言说明参考:https://github.com/tokio-rs/byteshttps://zhuanlan.zhihu.com/p/109977513目标之前阅读的部分,都......