首页 > 编程语言 >0045-Bytes-bytes源码阅读

0045-Bytes-bytes源码阅读

时间:2022-09-30 20:37:16浏览次数:82  
标签:bytes Bytes 源码 let println main fn

环境

  • Time 2022-05-29
  • Rust 1.61.0
  • Bytes 1.1.0

前言

说明

参考:https://docs.rs/bytes/latest/bytes/struct.Bytes.html

目标

Bytes 的源码基本上看完了,简单使用一下其中的方法。

new

fn main() {
    let bytes = Bytes::new();
    println!("{:?}", bytes);
}

from_static

fn main() {
    let bytes = Bytes::from_static(b"JiangBo");
    println!("{:?}", bytes);
}

len

fn main() {
    let bytes = Bytes::from_static(b"JiangBo");
    println!("{:?}", bytes.len());
}

copy_from_slice

fn main() {
    let bytes = Bytes::copy_from_slice(&b"JiangBo"[..5]);
    println!("{:?}", bytes);
}

slice_ref

fn main() {
    let bytes = Bytes::from_static(b"JiangBo");
    let slice = &bytes.as_ref()[..5];
    let sub = bytes.slice_ref(slice);
    println!("{:?}", sub);
}

split_off

fn main() {
    let mut b1 = Bytes::from_static(b"JiangBo");
    let b2 = b1.split_off(5);
    println!("{:?}", b1);
    println!("{:?}", b2);
}

split_to

fn main() {
    let mut b1 = Bytes::from_static(b"JiangBo");
    let b2 = b1.split_to(5);
    println!("{:?}", b1);
    println!("{:?}", b2);
}

truncate

fn main() {
    let mut bytes = Bytes::from_static(b"JiangBo");
    bytes.truncate(5);
    println!("{:?}", bytes);
}

clear

fn main() {
    let mut bytes = Bytes::from_static(b"JiangBo");
    bytes.clear();
    println!("{:?}", bytes);
}

总结

使用了 Bytes 中定义的一些方法。

附录

标签:bytes,Bytes,源码,let,println,main,fn
From: https://www.cnblogs.com/jiangbo4444/p/16746017.html

相关文章