Can a function mutate data AND return a boolean?

Spoiler alert: naive beginner question

I’m working on a Tetris implementation as a training and my dream would be to write this kind of function:

impl Game {
    fn move_piece_around(&mut self) -> bool {
        //
        // some code that mutates the data
        //
        if data_got_mutated {
            return true
        } else {
            return false
        }
    }

Is there a way I can do that ? Or do I have to rely on some kind of Option<_, _> ?

That syntax is valid, and you could simplify it even further to:

impl Game {
    fn move_piece_around(&mut self) -> bool {
        //
        // some code that mutates the data
        //
        
        data_got_mutated
    }
}

That said, you’ll have to keep track of whether the data got mutated yourself, there’s not a built in flag you can check.

Thank you very much, I was afraid of performing heresy and now I’m not!
So if I do

let myboolean: bool = self.move_piece_around();

Is that a call to the function ?

Yep! You wouldn’t need to explicitly state the type, Rust can infer that:

let myboolean = self.move_piece_around();

I would be a bit wary about relying too heavily on returning stuff from mutating functions (generally, it’s a good idea to make it clear whether a function is performing an action or getting a value, rather than mixing the two), but in situations like this where you need to signal the outcome of the mutation, it’s fine in my opinion.

2 Likes

Thank you very much that made my day.

1 Like

No problem :slight_smile:

I’d recommend installing Clippy if you haven’t already, it’ll catch a lot of the common issues/bad practices beginners run into with Rust.

1 Like

I’d also recommend the Rust Playground for when you want to play around with small bits of code to see what works. It has clippy under tools as well as rustfmt if you want your code formatted to an opinionated standard.

1 Like

I already use rustfmt and I love it. Now I really dislike reading unformatted code.
I just installed Clippy and I’ll definitely use it more. As to the Playground, I’m not sure. Yandros did write code for me there and it looks cool, however I prefer my good old IDE (Atom, but it lags, I’ll switch for VScode I guess). I like to hit save and cargo run.

To further reassure you that what you’re doing is reasonable, I’d like to point out the standard library operation HashSet::insert, which behaves much like your function.

The playground is not meant as a proper development environment so much as a way to quickly share sandboxed, runnable code snippets with others. It’s useful for discussions on here.

FYI - I also had trouble getting started due to wanting lots of feedback in the IDE and fighting the lag.

There’s a separate thread detailing some of that journey here, might be helpful: Windows / rls slow

(TL;DR - I now keep a separate window which runs cargo check / build / etc. via watchexec and then the IDE is more for just editing text, basically, which suits me nicely since I like Vim)

This watchexec thing is cool. I was refactoring, reading at the same time, and suddenly - bam! Tetris.