Search code examples
rustgtkglib

How to create a GString from a String?


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?
}

Solution

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