Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0613· OpenAIHugging Face Arena Human Preference 55K

write a program in Rust that shows "Hello world" text in a GTK4 window

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with starling-lm-7b-alpha’s answer.
  1. User

    write a program in Rust that shows "Hello world" text in a GTK4 window

  2. Assistant

    To create a simple "Hello World" application using GTK4 in Rust, you will first need to add the gtk crate to your Cargo.toml file.

    [package]
    name = "hello_world_gtk"
    version = "0.1.0"
    edition = "2021"
    
    [dependencies]
    gtk = { version = "0.9", features = ["v4_0"] }
    glib = "0.10"
    

    Next, create a main.rs file with the following code:

    use gtk::prelude::*;
    use gtk::{Application, ApplicationWindow, Builder, Label};
    
    fn main() {
        let application = Application::new(
            Some("com.example.HelloWorld"),
            Default::default(),
        );
    
        application.connect_activate(|app| {
            let builder = Builder::from_string(include_str!("ui/hello_world.ui"));
            let window: ApplicationWindow = builder.object("window").expect("Couldn't get window");
            window.set_application(Some(app));
            window.present();
        });
    
        application.run();
    }
    

    This code creates a basic GTK application with an application window.

    Then you need to create a ui/hello_world.ui file with the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <interface>
      <requires lib="gtk" version="4.0"/>
      <object class="GtkApplicationWindow" id="window">
        <property name="title" translatable="yes">Hello, World!</property>
        <property name="default-width">200</property>
        <property name="default-height">200</property>
        <child>
          <object class="GtkLabel">
            <property name="label" translatable="yes">Hello, World!</property>
          </object>
        </child>
      </object>
    </interface>
    

    This XML file defines the user interface for your application. It specifies a window with a title of "Hello, World!", and a label inside the window that also says "Hello, World!".

    Finally, use cargo run to run your application.

    Please note that you need to have GTK4 installed on your machine to compile and run this program.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.