Format raw strings in rust

When refactoring code to make API call to Gemini, I learnt how to format raw strings in rust. fn main() { let var1 = "test1"; let formatted = format!(r#"var1 is: {}"#, var1); println!("{}", formatted); } This gives output as : var1 is: test1 If you want { in the output, then it needs to be escaped with additional { like: fn main() { let var1 = "test1"; let formatted = format!

Continue Reading »

How to use .env files with your rust project

It is very important that secrets are stored in environment variables during the runtime. During the development, it makes sense to use .env file. In my last project, there were probably 40+ variables in the .env. Although that project was in RoR the idea of .env itself is not new. But for me, using .env with rust is new. For my current (self) assignment, I needed to store an API Key.

Continue Reading »

Clippy

Clippy is a linter for Rust programming language. If you are annoyed by the compiler (shouting at telling you how your code is wrong), wait till you install and use clippy 😄 Jokes apart, why I want to use clippy is it tells us about idiomatic rust and can autofix issues (if we tell it to do so) Couple of fun facts I discovered : First search result for clippy is not what I was looking for 😄 till I searched for rust clippy clippy can not be installed via cargo install (As I tried initially) (As with rest of the rust ecosystem) there was a helpful error message with solution 😇 error: Clippy is no longer available via crates.

Continue Reading »

100 Days of Rust : Day 9 (Testing)

I continued reading Command Line Applications in Rust Learnt that testing is easy. Any function that has #[test] above it, will be found (across any files) and used by cargo test Couple of interesting crates : exitcode It has quite well defined exit codes. They come from FreeBSD I wish other languages / frameworks had something similar proptest is a property testing framework Based on python’s Hypothesis I need to spend time actually trying this human-panic Generates report file on panic Shows nice (if a bit long) message to the user, asking them to (optionally) email the report file to the developer 🤯 Things to explore:

Continue Reading »

100 Days of Rust : Day 8

Today I started reading Command Line Applications in Rust Even though I have not finished reading “the book”, I am (by now) familiar with enough rust code that reading this book was kinda refreshing. Few important things I picked up : {:?} in println! is called debug representation (quite useful for .. debugging 😄) Custom data types can add support for {:?} for debugging and logging, one needs to add a #[derive(Debug) above their definition.

Continue Reading »

100 Days of Rust : Day 7

Technically this may be more like day 8 or 9, cause I did read some stuff from the rust book in last few days, and made note here Nothing improves your understanding better than doing – Me 😄 I was trying accessing the individual fields in tuple struct using dot notation via the index Since the rust book does not have an example of it, I used rust playground (Awesome resource BTW) and just printed stuff.

Continue Reading »

100 Days of Rust : Day 6 (Ownership)

When I started reading about Ownership, I was thinking I have done C. I understand memory But Rust book explains : If you are familiar with systems programming, you might think of memory at a low level like “memory is an array of bytes” or “memory is the pointers I get back from malloc”. .. The low-level model is too concrete to explain how Rust works. Rust does not allow you to interpret memory as an array of bytes, for instance.

Continue Reading »

100 Days of Rust : Day 5

I want to access Bard API via Rust, but it will take some time. Here are the things I did today. First, I tried (successfully) to access an URL via Rust code. ✅ I used the reqwest module. I just added this dependency in my Cargo.toml and (like in other languages) the dependency hell was let loose 😄 67 other modules were added to the Cargo.lock! I also learnt to use global variables in Rust.

Continue Reading »

100 Days of Rust : Day 4

Started with (optional) assignment at the end of “Programming Concept” chapter : Convert temperature between Celsius and Fahreinheit. 1 Since the algorithm itself is not part of the learning the language, I asked Google Bard about the formula. It was nice working on some code after a while (Aside from work, I mean) Once again, I realized that compiler is very helpful 😍 I started simple. Breaking down functionality into smaller functions.

Continue Reading »

100 Days of Rust : Day 3

Today I finished Functions and Control Flow etc. While these are not new “concepts” for me, one always learns something new when learning a new language. Two things I learnt: I liked the fact that rust does not evaluate anything other than boolean as truthy like both python and ruby loop can have labels, and can be used with break and continue to indicate where should the execution continue