Terminal Tetris

This is a rust port of tinytetris - the most starred c++ tetris repository on github. I like the tiny part in this project - tiny mostly refers to the source code which can be minified down to almost nothing.

This rust version is similar in source code size (unminified) - but I hope is more readable.

The UI (ncurses) is mostly the same - I have moved the controls to the arrow keys.
Internally the tetrominos are packed differently - fits neatly into 7 u64s, where the original used 28 i32s.

Questions

  • Functions width() and height() calculate tetromino width and height on the fly - can it be expressed shorter?

  • Is there an automatic minifier that works on rust code?

This is mostly a gimmick - but it is interesting that the c++ sources can be minimized as much as they can.

3 Likes

.fold(0, |m, v| std::cmp::max(m, v)) is .fold(0, std::cmp::max) or .max().unwrap_or(0), same for min. (try running clippy)

In draw_screen, you have a side-effectful map followed by for_each, which would idiomatically be written as for_each, or for (v, i) in g.board.iter().enumerate(), removing the error-prone 0..20.

For casting a u8 to a usize, I'd use .into(), which guarantees to not truncate.

2 Likes

Thanks - good points.

Found another way to make clippy happy ...

Great. You could also use .minmax() from the itertools crate.

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.