Skip to Content
Rust Programming By Example
book

Rust Programming By Example

by Guillaume Gomez, Antoni Boucher
January 2018
Beginner to intermediate
454 pages
10h 8m
English
Packt Publishing
Content preview from Rust Programming By Example

Creating custom widgets

In object-oriented languages, it is very easy to create new widgets and use them like built-in widgets. In this paradigm, you only need to create a new class that inherits from a widget and that's it.

In Chapter 5, Creating a Music Player, we created custom widgets, such as Playlist and MusicToolbar, but we needed to create a function to get the real GTK+ widget:

pub fn view(&self) -> &TreeView {
    &self.treeview
}

An alternative would have been to implement the Deref trait:

use std::ops::Deref;

impl Deref for Playlist {
    type Target = TreeView;

    fn deref(&self) -> &TreeView {
        &self.treeview
    }
}

That implementation would allow us to add the widget to its parent like this:

parent.add(&*playlist);

(Note the leading * in ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rust Programming Cookbook

Rust Programming Cookbook

Claus Matzinger
Rust Web Programming

Rust Web Programming

Maxwell Flitton

Publisher Resources

ISBN: 9781788390637Supplemental Content