rust - 如何从 std::string::String 获取 bytes::bytes::Bytes?

标签 rust

我正在尝试使用 Rusoto 库调用 AWS Lambda 函数。该请求有一个 JSON 编码的有效负载,我目前将其作为一个字符串,但该库为此坚持使用 bytes::bytes::Bytes 结构。我还没有找到将字符串转换为字节的方法(这不是世界上最适合谷歌搜索的东西)——有人能帮我吗?谢谢。

expected struct `bytes::bytes::Bytes`, found struct `std::string::String`

最佳答案

Bytes工具 From/Into对于String允许从字符串转换为表示 UTF-8 中该字符串的字节:

use bytes::Bytes;
fn main() {
    let string = "démonstration".to_string();
    println!("{:?}", string); // "démonstration"
    let bytes: Bytes = string.into();
    println!("{:?}", bytes); // b"d\xc3\xa9monstration"
}

Try it in the playground

关于rust - 如何从 std::string::String 获取 bytes::bytes::Bytes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66551571/

相关文章:

rust - 重构解析器代码以避免借用检查器问题

rust - 第二个 gtk_main_quit() 不工作

function - Rust:具有相同名称的结构和函数

rust - eh_personality 在哪里调用?

rust - 如何跨平台获取文件的修改时间?

rust - 我如何初始化一个数组,以便 Rust 知道它是一个 `String` s 而不是 `str` 的数组?

rust - rust future `try_filter_map`导致 panic : 'async fn resumed after completion'

rust - 我可以通过在两个异步接收器上调用 select 来错过一个值吗?

struct - 如何在调试输出中打印完整的结构路径?

rust - 如何将 &Vec<Box<dyn Child>> 转换为 &Vec<Box<dyn Base>> where trait Child : Base {}?