我正在尝试使用 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"
}
关于rust - 如何从 std::string::String 获取 bytes::bytes::Bytes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66551571/