DEV Community

Cover image for Matrix code rain - in Rust
Mark Damics
Mark Damics

Posted on • Edited on

1

Matrix code rain - in Rust

Not long ago I started to work with Rust and for me it is amazing. The speed, the type safety, the number of use cases are just amazing in Rust.

As practice, I wanted to implement the Matrix code rain, which I always wanted to implement but never got there. It turned out to be a good little project. It includes algorithms and terminal manipulation.

My github repo: Matrix Rainfall

There are 3 different structs which represent each element:

Symbol: Here we store and edit each character of the matrix. The function of setting a random symbol is implemented here (it will be called in the Column). The fading function is also implemented here. For the color manipulation, I used a third-party crate.

Column: Here we initialize each symbol, and most of the algorithm (starting raindrops, space between raindrops, calling random characters, calling fade function) happens here. We store here each vector (raindrop) start point and end point.

Matrix: Practically, it represents a table where you have the terminal width and height. The setting of the background color, moving the cursor, and the initialization of the columns happen here.

With parameters, we can set the speed, the color of the background, and the color of the characters.

Green Matrix

Yellow on bluw Matrix

Blue Matrix

So far it works well with '1' and '0' characters but if I want to add more then the columns don't hold themselfs, like this:

error with multiple symbols

EDIT:
Now it works with multiple characters. The problem was this '日' character and the reason was that this character takes 2x of regular size on the terminal.

Reference:


repo

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay