英文:
How to create a GString from a String?
问题
我正在使用Rust与GLib,并需要创建一个GString。我该如何做?
fn example() -> GString {
let hello = "Hello";
// 我如何将hello作为GString返回?
}
英文:
I am using Rust with GLib and need to create a GString. How do I do that?
fn example() -> GString {
let hello = "Hello";
// How do I return hello as a GString?
}
答案1
得分: 2
使用文档化的函数glib::GString::from_string_unchecked:
GString::from_string_unchecked("Hello".into())
或者使用glib::gstr来创建&'static GStr,然后将其转换为拥有的类型:
let hello = glib::gstr!("Hello");
hello.to_owned()
英文:
Use the documented function glib::GString::from_string_unchecked:
GString::from_string_unchecked("Hello".into())
Or use glib::gstr to create a &'static GStr instead and turn it into something owned:
let hello = glib::gstr!("Hello");
hello.to_owned()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论