Is there any kotlin apply scope function for rust

In kotlin i can write

ui.apply {
   label(...)
   text_filed(...)
   button(...)
}

In rust, i can't omit the ui. or this

ui.label(...);
ui.text_field(...);
ui.button(...);

Is there any method to use the former style, or similar

I'm not aware of any such syntactical sugar to abstract multiple method calls to a single instance like this. See also this old topic. And there is the scope-functions crate that tries to mimic some of Kotlin's scope functions in Rust, though their version of apply wouldn't give you the desired syntax as it takes a closure as argument, so you'd still have to write something like:

ui.apply(|ui| {
    ui.label(...);
    ui.text_field(...);
    ui.button(...);
});

Rust doesn't support the implicit this/self even inside methods. I guess you could write a macro, but then every function call will become a method call on ui.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.