Rust : How to use `match` to replace nested if/else

When solving errors4.rs exercise of rustlings, I initially used nested if/else. It helps with readability.

While the solution was correct, clippy suggested following:

help: consider rewriting the if chain with match: match value.cmp(&0) {...}

I had used match only with various possible values (mostly from Enum) and thus was unfamiliar with value.cmp(&0) approach.

Continue Reading »

Announcing my rust project: snb

After learning (and I’m still learning) basics of Rust, I decided that I need to work on real world project to get better understanding of the language and face real problems 😄

Introducing snb 1 - Superfast notes from the terminal 🎉

Continue Reading »

Adding alias to CLI built in #rust

Turns out it is very easy if using clap

Continue Reading »

My PR related to Rust project got merged 🎉

I’m so happy that my PR 1 to a new and upcoming #rust framework called Cot was merged yesterday.

@m4tx - Thanks for accepting my contribution 🙏

Continue Reading »

Rust: Improve readability in default arm of match statement

#TIL in #rustlang we can use any variable name (starting with _) in ignore path of match

Continue Reading »

Rust Script

After my previous post I came across other ways to “quickly test rust code” Obvious being rust-script It is easy to install via cargo install rust-script (But I had it already installed. I think Espanso installed it, but not too sure) This may be better than rustc because one can define the dependencies in the “script” itself, like : #!/usr/bin/env rust-script //! Dependencies can be specified in the script file itself as follows: //!

Continue Reading »

Rust: When rustc Beats Cargo for quick trial code

Recently, I needed to test try some functionality (of getting user’s home directory) 1

Setting up a throw away project via cargo new is certainly possible but seemed overkill.

Continue Reading »

Rust: How to print Command Output

I was going thru struct Command in std::process 1

Obviously, I tried the very first code snippet in the rust playground. There is a very convinient ▸ to run the sample code.

The code ran without any errors.

But no output 🤔

Continue Reading »

Finally got Auto Complete working in Helix

I have been using Helix as an editor, not as an IDE. One of the USP of Helix is LSP support out of the box

Recently, I’ve been programming in rust.

Helix is also written in rust.

So I think it is natural that they would support rust LSP rust-analyzer and it does.

But I wasn’t getting auto completions 😞

Continue Reading »

Rust: Unit tests in a separate file

In rust, tests are written in the same file as the code. But I want to have the tests in a separate file. Conventionally, only the integration tests are written in files under tests folder. 1 I just wanted to have the unit tests in a separate file. So I created a file src/my_tests.rs and moved all the tests there. But cargo test kept saying running 0 tests Turns out, I’m not the only one.

Continue Reading »