Rust adw::Bin set child to None

So if I understand it correctly the way to empty a adw::Bin is to set it’s child to None. However None doesn’t implement IsA<gtk::Widget>. So is there a way to do this properly?

let bin = adw::Bin::new();
bin.set_child(None);  // <- cannot satisfy `_: IsA<gtk4::Widget>` 

You need a “typed” None; you can either or None::<&gtk::Widget> or gtk::Widget::NONE. The wrapper classes provided by the Rust bindings provide a NONE constant value for this particular case.

1 Like

To give some background why something like None is actually typed, consider the following code

fn unwrap_or_default<T: Default>(v: Option<T>) -> T {
    match v {
        Some(v) => v,
        None => T::default(),

let x: Option<u32> = None;
let y: Option<String> = None;

println!("{}", unwrap_or_default(x));     // Calls into u32's Default impl
println!("{}", unwrap_or_default(y));     // Calls into String's Default impl
println!("{}", unwrap_or_default(None));  // Impossible to know which Default impl should be used here

For structs/etc it’s possible to give default generic types but for functions this is unfortunately not possible yet/anymore. Tracking issue for `invalid_type_param_default` compatibility lint · Issue #36887 · rust-lang/rust · GitHub has some background about that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.