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

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

How to create a GString from a String?

问题

我正在使用Rust与GLib,并需要创建一个GString。我该如何做?

  1. fn example() -> GString {
  2. let hello = "Hello";
  3. // 我如何将hello作为GString返回?
  4. }
英文:

I am using Rust with GLib and need to create a GString. How do I do that?

  1. fn example() -> GString {
  2. let hello = "Hello";
  3. // How do I return hello as a GString?
  4. }

答案1

得分: 2

使用文档化的函数glib::GString::from_string_unchecked

  1. GString::from_string_unchecked("Hello".into())

或者使用glib::gstr来创建&'static GStr,然后将其转换为拥有的类型:

  1. let hello = glib::gstr!("Hello");
  2. hello.to_owned()
英文:

Use the documented function glib::GString::from_string_unchecked:

  1. GString::from_string_unchecked("Hello".into())

Or use glib::gstr to create a &'static GStr instead and turn it into something owned:

  1. let hello = glib::gstr!("Hello");
  2. 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:

确定