如何从一个字符串创建一个GString?

huangapple go评论63阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年3月3日 20:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定