【Rust】字符串(一)

环境

  • Time 2022-03-24
  • Rust 1.59.0

示例

new

fn main() {
    let s = String::new();
    println!("{:?}", s.capacity());
}

with_capacity

fn main() {
    let s = String::with_capacity(4);
    println!("{:?}", s.capacity());
}

from_utf8

fn main() {
    let heart = vec![240, 159, 146, 150];
    let heart = String::from_utf8(heart).unwrap();
    println!("{heart:?}");
}

from_utf8_lossy

fn main() {
    let heart = vec![240, 159, 146, 150];
    let heart = String::from_utf8_lossy(&heart);
    println!("{heart:?}");
}

from_utf8_unchecked

fn main() {
    let heart = vec![240, 159, 146, 150];
    let heart = unsafe { String::from_utf8_unchecked(heart) };
    println!("{heart:?}");
}

into_bytes

fn main() {
    let name = String::from("JiangBo");
    let name = name.into_bytes();
    println!("{name:?}");
}

as_str

fn main() {
    let name = String::from("JiangBo");
    let name = name.as_str();
    println!("{name:?}");
}

make_ascii_lowercase

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

push_str

fn main() {
    let mut name = String::from("Jiang");
    name.push_str("Bo");
    println!("{name:?}");
}

总结

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

附录

posted @ 2022-05-29 07:24  jiangbo4444  阅读(74)  评论(0编辑  收藏  举报