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<_, _> ?
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.
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.
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)