Return relevant data type instead of Unit type ()

As I am new to Rust, I am not sure how to phrase the question meaningfully for future search and references, so I may need some help on the title.
Anyway, I have a piece of code implementing fltk-rust library:

rgb_img.scale(WIDTH/2, HEIGHT/2, false, true);

Which for my intent and purpose, works fine. However, I am trying to do a one-liner. Not sure it is an idiomatic way in Rust but anyway, the following code does not work:

frame.set_image(Some(rgb_img.scale(WIDTH/2, HEIGHT/2, false, true)));

I got

frame.set_image(Some(rgb_img.scale(WIDTH/2, HEIGHT/2, false, true)));
    |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `fltk::ImageExt` is not implemented for `()`

So yeah, I presume it is due to the scale method does to have the semi-colon in the end to catch the Unit-type value?
I mean 1 more line does not break the code or anything, but I am just curious.
Please enlighten me on this, and is there a one-liner solution to this? Thank you.

The scale method returns (), so you can't use its result to do anything interesting, and there's no point in passing that to another function. In order for this to be possible the fltk crate needs to define it to return something more interesting, but it probably won't because that method comes from a trait, and copying the implementer might be expensive (or impossible,) and returning a reference would be redundant, since you already have one.

1 Like

Is there an oneliner? Yes.

frame.set_image(Some({rgb_img.scale(WIDTH/2, HEIGHT/2, false, true); rgb_img}));

Should you do it? Nah, just use two lines.

The above works, because if we expand the block:

    rgb_img.scale(WIDTH/2, HEIGHT/2, false, true);

Then the value passed to Some is the value of the block, and the value of the block is the last expression in the block.


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.