Posted on July 28, 2019

I’m neither an experienced Rust programmer nor GTK developer. I don’t even have that much experience with building native GUIs or with system level programming. Hell, I might be the least qualified guy on the internet to write this post. Despite that, on more than just one occasion, I wondered what implications Rust’s static analysis of memory access has on writing GUIs.

GUI development is a special beast. I consider it to be an interesting use-case as the GUI development is usually associated with:

Threading or Concurrency (or both)

Long living application state and mutations

in case of native UI, linking to sh*tload of C/C++ libraries

imperative, often object oriented, APIs.

I thought it might be interesting to check implications which Rust’s borrow and ownership checker has in those types of applications as clearly, even in the most simple GUI, one simply must run into some complications. That’s exactly what I’m going to do and document here. I’m going to implement the most simple (and useless) GTK application in Rust just for the sake of feeling the pain of fighting the compiler as an inexperienced user.

Boring but Necessary Setup

I’ve chosen GTK only because I’m developing this on Linux and I have a feeling that C lib will probably have better bindings than C++ one would have (QT). First thing we will need to deal with is an installation of all libraries. I’m going to use the nix package manager because it seems to me like the only sane way of managing this. Setup should be compatible across Linux and Mac. If you’re trying to replicate this on Windows, good luck following this.

We’re going to create shell.nix file to define development environment containing all the tools we need. Note that I’m using nixos-unstable channel myself.

{ pkgs ? import < nixpkgs > { } }: ? importnixpkgs}: with pkgs ; pkgs mkShell { buildInputs = [ cargo rustc cairo gtk3 glib pkgconfig ] ; = [ cargo rustc cairo gtk3 glib pkgconfig ] }

Now, running $ nix-shell should bring us to the bash shell with all the native dependencies including rustc and cargo installed.

Use cargo to bootstrap the project within nix-shell:

[ nix-shell ] cargo init ] cargo init

to confirm that compilation works, we can run the hello world project generated by cargo.

[ nix-shell ] cargo run ] cargo run Compiling rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) Finished dev [unoptimized + debuginfo] target(s) in 0.39s dev [unoptimized + debuginfo] target(s) Running ` target/debug/rust-gtk-counter ` Hello , world! , world!

Because I don’t really have an idea what I’m doing, I’m going to just copy example Cargo.toml.

[ package ] name = "rust-gtk-counter" version = "0.1.0" authors = [ "Marek Fajkus <marek.faj@gmail.com>" ] = [ edition = "2018" [ dependencies.gtk ] version = "0.7.0" features = [ "v3_16" ] = [ [ dependencies.gio ] version = "" features = [ "v2_44" ] = [

I also recommend running cargo build right away as at this point, it will need to pull deps, do the linking and compilation, and there is really no point in waiting for all of this later.

UI Code

With help of GTK documentation and rust-gtk documentation, I was able to hack together world’s most embarrassing interface, yay. I’m just going to put the code here as there is really nothing that interesting in it. I put all the code to src/main.rs and even main procedure because why the hell not.

extern crate gtk ; gtk extern crate gio ; gio use std::env:: args ; args use gtk::prelude:: *; use gio::prelude:: *; use gtk:: { Application , ApplicationWindow , Button , Box , Label ,}; ApplicationApplicationWindowButtonLabel fn main() { main() // The simplest way to initalize new application let application = Application:: new( None , Default :: default ()) applicationnew(()) . expect( "failed to initialize GTK application" ) ; expect( // UI initialization . connect_activate( | app | { applicationconnect_activate(app // Window let window = ApplicationWindow:: new(app) ; windownew(app) . set_title( "First GTK Program" ) ; windowset_title( . set_default_size( 350 , 70 ) ; windowset_default_size( // Containers let container = Box :: new( gtk::Orientation:: Vertical , 3 ) ; containernew(Vertical // Header let header = gtk::HeaderBar:: new() ; headernew() let label = Label:: new( Some ( "Starting at 0" )) ; labelnew()) // Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( let dec_btn = Button:: new_with_label( "Decrement" ) ; dec_btnnew_with_label( // Compose piece together . add( & label) ; headeradd(label) . add( & header) ; containeradd(header) . add( & inc_btn) ; containeradd(inc_btn) . add( & dec_btn) ; containeradd(dec_btn) . add( & container) ; windowadd(container) // Display all widgets . show_all() ; windowshow_all() } ) ; // Passing arguments to the app . run( & args() . collect:: < Vec < _ >> ()) ; applicationrun(args()()) }

And this is how the app looks like when I run $ cargo run . I’m using tiling window manager (XMonad) so this will probably look slightly different on most other desktops.

Adding Actions

The application is not doing much. It’s doing exactly nothing (other than rendering useless window with useless widgets). Let’s try doing the most useless thing in the world like changing the title whenever we click on one of buttons.

Let’s just try what seems like the most simple way of doing this - changing content section to something like this:

// Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( . connect_clicked( | _ | { inc_btnconnect_clicked( . set_label( "Incremented" ) ; labelset_label( } ) ; let dec_btn = Button:: new_with_label( "Decrement" ) ; dec_btnnew_with_label( . connect_clicked( | _ | { dec_btnconnect_clicked( . set_label( "Decremented" ) ; labelset_label( } ) ;

[ nix-shell ] cargo run ] cargo run error [E0373]: closure may outlive the current function, but it borrows ` label ` , which is owned by the current function [E0373]: closure may outlive the current function, but it borrows, which is owned by the current function -- > src/main.rs:31:33 src/main.rs:31:33 | 31 | inc_btn.connect_clicked ( | _ | { | ^^^ may outlive borrowed value ` label ` ^^^outlive borrowed value 32 | label.set_label ( "Incremented" ); ); | ----- ` label ` is borrowed here is borrowed here | note : function requires argument type to outlive ` 'static` : function requires argument type to outlive --> src/main.rs:31:9 | 31 | / inc_btn.connect_clicked(|_| { 32 | | label.set_label("Incremented"); 33 | | }); | |__________^ help: to force the closure to take ownership of `label` (and any other referenced variables), use the `move` keyword | 31 | inc_btn.connect_clicked(move |_| { | ^^^^^^^^ ....

Of course… We don’t have GC to “track the references” to label for us so we need to somehow make sure it’s available for as long as we need to use that reference. That is whenever the connect_clicked closure will evaluate. But we have a hint about using move to transfer ownership of label so let’s just add it.

// Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( . connect_clicked( move | _ | { inc_btnconnect_clicked( . set_label( "Incremented" ) ; labelset_label( } ) ; let dec_btn = Button:: new_with_label( "Decrement" ) ; dec_btnnew_with_label( . connect_clicked( move | _ | { dec_btnconnect_clicked( . set_label( "Decremented" ) ; labelset_label( } ) ;

And we get another error.

[ nix-shell ] cargo run ] cargo run Compiling rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) error [E0382]: use of moved value: ` label ` [E0382]: use of moved value: -- > src/main.rs:35:33 src/main.rs:35:33 | 27 | let label = Label::new(Some( "Starting at 0" )); label = Label::new(Some()); | ----- move occurs because ` label ` has type ` gtk ::Label ` , which does not implement the ` Copy ` trait move occurs becausehas type::Label, which does not implement thetrait ... 31 | inc_btn.connect_clicked (move | _ | { (move | -------- value moved into closure here value moved into closure here 32 | label.set_label ( "Incremented" ); ); | ----- variable moved due to use in closure variable moved due to use in closure ... 35 | dec_btn.connect_clicked (move | _ | { (move | ^^^^^^^^ value used here after move ^^^^^^^^used here after move 36 | label.set_label ( "Decremented" ); ); | ----- use occurs due to use in closure use occurs due to use in closure

Maybe obviously, we can’t simply move ownership to multiple places. But maybe we can create copy of label reference for each button like this:

// Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() . connect_clicked( move | _ | { inc_btnconnect_clicked( . set_label( "Incremented" ) ; label_cloneset_label( } ) ; } let dec_btn = Button:: new_with_label( "Decrement" ) ; dec_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() . connect_clicked( move | _ | { dec_btnconnect_clicked( . set_label( "Decremented" ) ; label_cloneset_label( } ) ; }

I’m using blocks to create the nested namespace so I don’t have to be that creative with naming. But more importantly, the code above compiles and even works!

Some might expect that this code won’t work as we create independent clones of gtk::Label . I believe this type (as any other Widget) is just a reference to some GTK object so cloning it means creating just another reference pointing to the same GTK runtime thing. You can try to add the same thing or a clone of the same thing multiple times to the window. It won’t work the second time and print runtime warning to the stderr. ( rust-gtk-counter :16318 ) : Gtk-WARNING **: 17:54:30.775: Attempting to add a widget with type GtkLabel to a container of type GtkBox, but the widget is already inside a container of type GtkHeaderBar, please remove the widget from its existing container first. :16318Gtk-WARNING **: 17:54:30.775: Attempting to add a widget with type GtkLabel to a container of type GtkBox, but the widget is already inside a container of type GtkHeaderBar, please remove the widget from its existing container first.

Adding the State

Now, in order to raise our app from the absolute bottom of uselessness to just a bottom of it, we will need to introduce some state. And what is better than good old integer. And since we’re not building just any stupid application which does nothing, and thus can be implemented in a language like Haskell, we just reach for the only enterprise approved technique - the mutation.

Let’s be naive and just add our mutable state to the app for instance somewhere here…

// Containers let container = Box :: new( gtk::Orientation:: Vertical , 3 ) ; containernew(Vertical // State let mut counter = 0 ; counter

…and mutate it in handlers. I’m doing so only for increment button for now, why I’m doing so will be more apparent later on.

// Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() . connect_clicked( move | _ | { inc_btnconnect_clicked( += 1 ; counter . set_label( "Incremented" ) ; label_cloneset_label( } ) ; }

Boom! The compiler hates us again:

[ nix-shell ] cargo run ] cargo run error [E0594]: cannot assign to ` counter ` , as it is a captured variable in a ` Fn ` closure [E0594]: cannot assign to, as it is a captured variable in aclosure -- > src/main.rs:37:17 src/main.rs:37:17 | 37 | counter += 1 ; += 1 | ^^^^^^^^^^^^ cannot assign ^^^^^^^^^^^^assign | help : consider changing this to accept closures that implement ` FnMut ` : consider changing this to accept closures that implement -- > src/main.rs:36:37 src/main.rs:36:37 | 36 | inc_btn.connect_clicked (move | _ | { (move | _____________________________________ ^ 37 | | counter += 1 ; += 1 38 | | label_clone.set_label ( "Incremented" ); ); 39 | | } ); ); | | _____________ ^

And this time with a bit trickier error it seems.

I’m honestly not sure what compiler’s help is trying to tell us to do but I think it might be a good idea to encapsulate our mutation in something perhaps. After some digging in documentation I’ve found std::sync::atomic which seems like it might be a good tool for the job. There is even AtomicIsize but since it looks like this will make our “state management” a bit more verbose, let’s try to decouple it.

We can also remove our state : i32 as this won’t take us anywhere it seems.

We’re still in the main.rs (because I can’t import and I’m lazy). In my case, I’m going to put this above the main procedure.

use std::sync::atomic:: { AtomicIsize , Ordering }; AtomicIsizeOrdering struct Counter(AtomicIsize) ; Counter(AtomicIsize) impl Counter { Counter fn new(init : isize ) -> Counter { new(initCounter AtomicIsize:: new(init)) Counter(new(init)) } fn increment( & self ) -> isize { increment( let old = self . 0 . fetch_add( 1 , Ordering:: SeqCst) ; oldfetch_add(SeqCst) + 1 old } }

As you can see, we’re doing bunch of interesting things here. The essential idea is to use atomic operations to alter the state. If you’re interested in details, I recommend you study the documentation of std::sync::atomic. Now it’s a time to integrate our glorified integer into application starting with initialization of counter value.

// Containers let container = Box :: new( gtk::Orientation:: Vertical , 3 ) ; containernew(Vertical // State let counter = Counter:: new( 0 ) ; counternew(

And of course, click handler closure:

// Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() . connect_clicked( move | _ | { inc_btnconnect_clicked( let val = counter . increment() ; valcounterincrement() . set_label( & format! ( "Incremented to {}" , val)) ; label_cloneset_label(val)) } ) ; }

We also put the actual value into the label when we’re at it. And since this works great, we go and extend this to work with decrement button as well.

First, the new procedure to decrement the value:

fn decrement( & self ) -> isize { decrement( let old = self . 0 . fetch_sub( 1 , Ordering:: SeqCst) ; oldfetch_sub(SeqCst) - 1 old }

and then use it in decrement button clicked closure:

let dec_btn = Button:: new_with_label( "Decrement" ) ; dec_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() . connect_clicked( move | _ | { dec_btnconnect_clicked( let val = counter . decrement() ; valcounterdecrement() . set_label( & format! ( "Decremented to {}" , val)) ; label_cloneset_label(val)) } ) ; }

But wait what?

Compiling rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) error [E0382]: use of moved value: ` counter ` [E0382]: use of moved value: -- > src/main.rs:66:37 src/main.rs:66:37 | 48 | let counter = Counter::new(0); counter = Counter::new(0); | ------- move occurs because ` counter ` has type ` Counter ` , which does not implement the ` Copy ` trait move occurs becausehas type, which does not implement thetrait ... 58 | inc_btn.connect_clicked (move | _ | { (move | -------- value moved into closure here value moved into closure here 59 | let val = counter.increment(); val = counter.increment(); | ------- variable moved due to use in closure variable moved due to use in closure ... 66 | dec_btn.connect_clicked (move | _ | { (move | ^^^^^^^^ value used here after move ^^^^^^^^used here after move 67 | let val = counter.decrement(); val = counter.decrement(); | ------- use occurs due to use in closure use occurs due to use in closure error : aborting due to previous error : aborting due to previous error

There is another problem with moving the value but it seems we might have a chance to fix it by simply deriving the Copy instance for our Counter struct, let’s see.

#[ derive ( Copy )] derive struct Counter(AtomicIsize) ; Counter(AtomicIsize)

and try to compile it…

[ nix-shell ] cargo run ] cargo run Compiling rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) rust-gtk-counter v0.1.0 (/home/marek/Projects/rust-gtk-counter) error [E0204]: the trait ` Copy ` may not be implemented for this type [E0204]: the traitmay not be implemented for this type -- > src/main.rs:11:10 src/main.rs:11:10 | 11 | #[ derive (Copy)] #[(Copy)] | ^^^^ ^^^^ 12 | struct Counter(AtomicIsize); Counter(AtomicIsize); | ----------- this field does not implement ` Copy ` this field does not implement error : aborting due to previous error : aborting due to previous error

Not that surprising. Our atomic type doesn’t implement Copy . This won’t take us anywhere, let’s try the last resort - the documentation.

Luckily, std lib offers this reference counting primitive called std::sync::Arc which seems to offer the semantics we need.

The type Arc<T> provides shared ownership of a value of type T, allocated in the heap. Invoking clone on Arc produces a new Arc instance, which points to the same value on the heap as the source Arc, while increasing a reference count. When the last Arc pointer to a given value is destroyed, the pointed-to value is also destroyed.

We obviously want to point to the same memory if we want to be decrementing the same value we’re incrementing and reference counting will make sure this shared mutable state is available as long as it’s needed by anything just like if we had a GC.

First, we need to change the definition of the counter variable:

// State use std::sync:: Arc ; Arc let counter = Arc:: new( Counter:: new( 0 )) ; counternew(new())

so we can clone the counter in both closures (even though technically, cloning it in just one of them should be enough).

// Content let inc_btn = Button:: new_with_label( "Increment" ) ; inc_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() let counter_clone = counter . clone() ; counter_clonecounterclone() . connect_clicked( move | _ | { inc_btnconnect_clicked( let val = counter_clone . increment() ; valcounter_cloneincrement() . set_label( & format! ( "Incremented to {}" , val)) ; label_cloneset_label(val)) } ) ; } let dec_btn = Button:: new_with_label( "Decrement" ) ; dec_btnnew_with_label( { let label_clone = label . clone() ; label_clonelabelclone() let counter_clone = counter . clone() ; counter_clonecounterclone() . connect_clicked( move | _ | { dec_btnconnect_clicked( let val = counter_clone . decrement() ; valcounter_clonedecrement() . set_label( & format! ( "Decremented to {}" , val)) ; label_cloneset_label(val)) } ) ; }

And our awesome application compiles and runs again!

Additional Cleaning

This is really all the important stuff but there are some additional cosmetic details I just must do because of my OCD. So just quickly.

First, the code. We should really read the initial state as well so let’s add the simple getter.

fn get( & self ) -> isize { get( self . 0 . load( Ordering:: SeqCst) load(SeqCst) }

And use it to display the initial title…

// Header let header = gtk::HeaderBar:: new() ; headernew() let label = Label:: new( Some ( & format! ( "Starting at {}" , counter . get()))) ; labelnew(counterget())))

And because we want everyone to be able to enjoy our glorious application, we can provide nix based build and installation. Create the new file called default.nix (or some prefer release.nix ).

let pkgs = (import < nixpkgs > {}) .fetchFromGitHub { = (importnixpkgs{}) owner = "NixOS" ; repo = "nixpkgs-channels" ; rev = "b5f5c97f7d67a99b67731a8cfd3926f163c11857" ; # nixos-unstable as of 2019-07-24T18:57:18-05:00 sha256 = "1m9xb3z3jxh0xirdnik11z4hw95bzdz7a4p3ab7y392345jk1wgm" ; }; in with (import pkgs {}); (import pkgs {}); rustPackages.rustPlatform.buildRustPackage rec { rec { name = "rust-gtk-counter- ${version} " ; version = "0.1.0" ; src = ./. ; = ./. cargoSha256 = "0i4px1k23ymq7k3jp6y5g7dz0ysjzwrqqxfz4xg399y7zg5wwwhr" ; nativeBuildInputs = [ pkgconfig ] ; = [ pkgconfig ] buildInputs = [ cairo gtk3 glib pkgconfig ] ; = [ cairo gtk3 glib pkgconfig ] }

Unlike in shell.nix , where we used the system’s global <nixpkgs> , here we use global ones only for fetchFromGitHub and fetch exact revision of nixpkgs to make sure that we get the same version of dependencies in the future.

Now we can use (even outside of nix-shell) $ nix-build to build the whole project (and likely compile all the C as well as Rust from source) which will produce symlink named result in local directory pointing to compiled version of our package in nix-store linked to correct dependencies. $ ./result/bin/rust-gtk-counter should start the app. We should also add result to our .gitignore . You can even install the app to your nix-env with $ nix-env -if . because one never knows when (s)he will need to count from zero to some number. The app will then be installed as rust-gtk-counter .

Since I have Mac as well, I can try that nix-build is working on MacOS as expected.

Enjoy glib compile time if you try this.

Conclusion

I have to admit that I expected this whole process to be even slightly more challenging for someone with so little experience with both Rust and GTK as me. It was not all roses and rainbows though. I’ve definitely spent a lot of time reading documentation, trying to figure out what to use and how in order to get to the desired outcome. Also, our application is miles away from being advanced or even useful.

I have to admit that Rust brings some reasonable trade-offs when it comes to building a GTK application. This doesn’t mean that it’s for free though. If you expect to write the code in the same way as you would do using some binding to language with GC (like Python or JavaScript) or probably even idiomatic GTK C, you’re likely to hit some walls on the way. If you decide to use Rust for GTK application development anyway, you might want to check relm for higher level API before you start designing your own framework. If you’re a Haskeller, you should check the gi-gtk-declarative but be aware that both of those libraries are alpha. Also for you might want to check mmstick’s comment on Reddit and PopOS’ Firmware Manager source to learn about more advanced pattern of designing GTK app in Rust. And last but not least if you’re using Windows definitely check druid which is a data oriented UI toolkit written in Rust. This is the implementation of druid application similar to this one.

Will I use Rust to build some useful GTK application myself? Maybe I will! But I will definitely evaluate both Rust and some GC language (probably Haskell) and choose the more appropriate tool to do the job based on the business logic.

The whole code is available on GitHub.