zoukankan      html  css  js  c++  java
  • 【Rust】字节数组

    环境

    • Rust 1.56.1
    • VSCode 1.61.2

    概念

    参考:https://doc.rust-lang.org/rust-by-example/std/str.html

    示例

    在字符串前加上一个 b 来表示。

    main.rs

    use std::str;
    
    fn main() {
        let bytestring = b"this is a byte string";
    
        println!("A byte string: {:?}", bytestring);
    
        let escaped = b"\x52\x75\x73\x74 as bytes";
        println!("Some escaped bytes: {:?}", escaped);
    
        let raw_bytestring = br"\u{211D} is not escaped here";
        println!("{:?}", raw_bytestring);
    
        if let Ok(my_str) = str::from_utf8(raw_bytestring) {
            println!("And the same as text: '{}'", my_str);
        }
    
        let _quotes = br#"You can also use "fancier" formatting, \
                        like with normal raw strings"#;
    
        let shift_jis = b"\x82\xe6\x82\xa8\x82\xb1\x82\xbb"; // "ようこそ" in SHIFT-JIS
    
        match str::from_utf8(shift_jis) {
            Ok(my_str) => println!("Conversion successful: '{}'", my_str),
            Err(e) => println!("Conversion failed: {:?}", e),
        };
    }
    

    总结

    了解了 Rust 中字节数组的定义和使用方式。

    附录

  • 相关阅读:
    php分页问题
    php中memcached的使用
    Linux安装Git
    day06
    day07
    day03
    day05
    day04
    列表的操作
    初识数据类型
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/15745077.html
Copyright © 2011-2022 走看看