如何将std::string::String转换为bytes::bytes::Bytes?

3

我正在尝试使用Rusoto库调用AWS Lambda函数。请求具有JSON编码的有效负载,我目前将其作为字符串处理,但是库坚持要求使用bytes::bytes::Bytes结构体。我无法找到将字符串转换为字节的方法(这不是世界上最容易搜索的东西)- 有谁能帮我吗?谢谢。

expected struct `bytes::bytes::Bytes`, found struct `std::string::String`
1个回答

6

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"
}

在Playground中尝试


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接