其他分享
首页 > 其他分享> > 【Rust】字符串切片(一)

【Rust】字符串切片(一)

作者:互联网

环境

示例

len

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.len());
}

is_empty

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.is_empty());
}

is_char_boundary

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.is_char_boundary(5));
}

as_bytes

fn main() {
    let name = "JiangBo";
    let name = name.as_bytes();
    println!("{:?}", name);
}

as_bytes_mut

fn main() {
    let mut name = String::from("JiangBo");
    let name = unsafe { name.as_bytes_mut() };
    println!("{:?}", name);
}

as_ptr

fn main() {
    let name = "JiangBo";
    println!("{:p}", name.as_ptr());
}

as_mut_ptr

fn main() {
    let mut name = String::from("JiangBo");
    println!("{:p}", name.as_mut_ptr());
}

get

fn main() {
    let name = "JiangBo";
    println!("{:?}", name.get(5..));
}

get_mut

fn main() {
    let mut name = String::from("JiangBo");
    println!("{:?}", name.get_mut(5..));
}

总结

了解了字符串切片中相关的一些方法。

附录

标签:mut,name,JiangBo,切片,let,fn,字符串,main,Rust
来源: https://www.cnblogs.com/jiangbo4444/p/16322798.html