Quickly Set Your GTK Rust Environment & Start Coding!

Mar 27, 2018 · 4 min read

This is one of these posts that demo how easily a Getting Started can be, and hopefully will push some people to start with GTK & Rust development! I must admit Rust + GTK is quite bitchy combination, it is not easy, but perhaps is the only solution for GNOME apps that performance matters, and bindings are very active maintained from GNOME & Mozilla contributors! Actually I see Rust as the future of GTK applications development! Might be wrong though :p

So here we go! This is a copy/paste guide, there is nothing advanced, but you should always follow projects documentation! I’m just trying to demo the major advantage of Rust! It is DAMN EASY TO START WITH!

Get Rust!

I will use Nightly Rust here, because it brings some improvements on RLS, plus there is backwards compatibility

$ curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightlyRustup Github

On Tilix (or any other TE) make sure you have “Run Command as Login Shell” checked. Open a new Terminal Session

$ rustc --version
rustc 1.26.0-nightly (482a913fb 2018-03-25)

Create a New Project!

Just to make sure everything’s okay!

$ cargo new hello_rust --bin
$ cd hello_rust
$ cargo run
Compiling hello_rust v0.1.0 (file:///home/alex/Projects/Demo/hello_rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.33 secs
Running `target/debug/hello_rust`
Hello, world!

Get RLS!

Thats the Rust Language Server that will add on editors things like code completion, types and documentation on hover, warnings & errors etc

$ rustup component add rls-preview rust-analysis rust-srcRLS on Github

Get a Code Editor!

You obviously can use any code editor you like, and as a matter of fact GNOME Builder has also support for RLS! But for first is better to go with VSCode that has a very good Rust support, and Rust plugin is written by Rustup guys

Get VSCode Insiders edition, press “Ctrl+P” and install Rust RLS Plugin

ext install rust-lang.rustVSCode RLS on Github

Now open the “hello_rust” project you created before, and on the bottom left of VSCode you should see an RLS indicator


You obviously need the GTK development files, and I assume you already know how to do that! It should be something like

$ sudo dnf install gtk3-devel glib2-devel

At this point to refer that GTK-RS doesn’t support yet GTK 4.0, which is a huge drawback, considering GTK 4.0 is gonna release (hopefully) in Sept, and everyone wants to use that already!

This is a basic GTK program in Rust, I wont explain the code, it doesn’t do anything anyway, but it demos how we construct applications with modules and structs, and how we can use custom styles! oh and Medium has messed up the format a bit :/

I’m using these files

  1. Cargo.toml
  2. src/main.rs
  3. src/components/header.rs
  4. src/components/mod.rs
  5. src/components/styles/app.css

On VSCode open “Cargo.toml” and add

name = "hello_rust"
version = "0.1.0"
authors = ["alex"]
gdk = "0.7.0"
gtk = { version = "0.3.0", features = ["v3_22"] }


extern crate gtk;
use gtk::*;
mod components;
use components::App;
fn main() {
let app = App::new();


use gtk::*;pub struct Header {
pub container: HeaderBar,
pub hello_btn: Button,
impl Header {
pub fn new() -> Header {
let container = HeaderBar::new();
container.set_title("Hello Rust!");
let hello_btn = Button::new_with_label("Hello!");
//add "hello-btn" class
hello_btn.get_style_context().map(|c| c.add_class("hello-btn"));
Header {


mod header;use self::header::Header;use gtk;
use gtk::*;
use std::process;
const CSS: &str = include_str!("styles/app.css");pub struct App {
pub window: Window,
pub header: Header,
impl App {
pub fn new() -> App {
if gtk::init().is_err() {
println!("failed to init GTK");
let window = Window::new(WindowType::Toplevel);
let header = Header::new();
//Add custom CSS
let screen = window.get_screen().unwrap();
let style = CssProvider::new();
let _ = CssProviderExt::load_from_data(&style, CSS.as_bytes());
StyleContext::add_provider_for_screen(&screen, &style, STYLE_PROVIDER_PRIORITY_USER);
window.set_default_size(600, 350);
window.connect_delete_event(move |_, _| {
App {


.hello-btn {
headerbar {
window {

Now you can give

$ cargo run

And Rust will download all dependencies, compiles them, and run the application! Pretty Neat!

Image for post
Image for post

If you enjoy that, you can now read Rust Book and go to Rust GTK Github, and do something useful :) On their official page, you will find lots of apps you can steal code from!

You can easily publish your Rust Applications on Flathub, using the Flatpak Rust Extension! Check out the Fractal GTK-Rust Recipe!

If you want to say/ask anything, please use G+

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store