How to set member callback function


hi, im using gtk-rs to write a gui application

extern crate gtk;

use gtk::traits::*;
use gtk::signal::Inhibit;

struct Test {
    window: gtk::Window,

impl Test {

    fn new() -> Option<Test> {
	let window = gtk::Window::new(gtk::WindowType::Toplevel).unwrap();
	let button = gtk::Button::new_with_label("button").unwrap();
	let layout = gtk::Box::new(gtk::Orientation::Vertical, 5).unwrap();

	button.connect_clicked(callback); // how to do this ?


	Some(Test {
	    window: window,

    fn callback(&self, _: gtk::Button) {
	println!("button clicked!");

fn main() {

    let t = Test::new().unwrap();


i want to connect a callback to member function, because i need to use “window” variable in Test struct.


Callbacks can be any function satisfying the signature (in this case being a function that takes a Button as an argument), just pass the function’s name. If you want to also pass some information you just calculated you can use a closure. Closures are explained in The Book’s chapter on closures