gpt4 book ai didi

string - 在 Rust 中如何从字符串创建 CString?

转载 作者:行者123 更新时间:2023-12-05 02:43:50 34 4
gpt4 key购买 nike

如何在 Rust 中从 String 创建 std::ffi::CString

假设 String 已经存储在一个可以在必要时移动的变量中,不是像许多构建 的示例中那样的文字CString.

我研究了 CString 的文档: https://doc.rust-lang.org/std/ffi/struct.CString.html

字符串: https://doc.rust-lang.org/std/string/struct.String.html

我还是没看到路径。您必须经历许多指针类型中的一种; IntoFrom 没有为这些类型实现,所以 .into() 不起作用。

最佳答案

String 工具 Into<Vec<u8>>已经:

use std::ffi::CString;


fn main() {
let ss = "Hello world".to_string();
let s = CString::new(ss).unwrap();
println!("{:?}", s);
}

Playground

关于string - 在 Rust 中如何从字符串创建 CString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66747717/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com