Rust : Use println! only in Debug Build

We all know we shouldn’t use print debugging, and yet we all do 😉 1 Jokes apart, when I’m still developing the code, I use the debugger where possible. But sometimes, I want to keep certain print statements to verify runtime behaviour, especially when rolling out new feature and when there are too many variations (some of them unknown) in incoming data. I’m aware, logging is the right way to handle this (with loglevel set to debug or something), but it seems too much when developing toy projects.

Continue Reading »

`Vec::with_capacity` in Rust

At first, I assumed since we’ve declared the capacity upfront, it would be maximum capacity of the Vec Turns out, since Vec is expected to shrink and grow, as needed, there is no maximum capacity for Vec It just ensures that “sufficient” memory is allocated to the Vec, such that memory reallocation is not required. On the other hand, if you need more that what you declared with with_capacity, you will get it, but there will need to be reallocation (of memory), so it will be inefficient.

Continue Reading »

Conditional Compilation in Rust

Today I learnt that certain rust code can be marked such that it is compiled only for specific platform. This makes sense for low level libraries that provide platform-specific functionality that is not available on other platforms. This is achieved by tagging the function with #[cfg(target_os = "xyz")] Here xyz can be one of the following : “windows” “macos” “ios” “linux” “android” “freebsd” “dragonfly” “openbsd” “netbsd” Similar to target_os, here are other options :

Continue Reading »

rust can Divide by Zero

At least for floating point numbers, it does not crash/panic! 🤷‍♂ fn main() { let x = 10.0; let y = 0.0; println!("{:?}", x/y); } This above code returns inf But if we change the number to int, compiler catches this 1 at shows the following error: error: this operation will panic at runtime This is a contrived example. Instead of static values, if these were passed at runtime, it would (should?

Continue Reading »

Rust: Clone vs Copy

One of the strength of Rust is memory management. This also leads to compiler errors related to move or borrow When we assign an existing variable to new variable, two things can happen. Either the data is copied - in that case we can use both old and the new variable without worry. 1 Or data is moved - now we can only use the new variable, as the old variable is “out of scope”.

Continue Reading »

error: Unknown binary 'rust-analyzer'

As I am setting up my new machine, I came across this error when emacs tried to use rust-analyzer error: Unknown binary 'rust-analyzer' in official toolchain 'stable-x86_64-apple-darwin'. As mentioned in this SO question, which rust-analyzer did not show any error.1 Luckily, the same SO question also had the solution I needed to explicitly install it via rustup component add rust-analyzer Luckily, the comment in the accepted answer also explained why which works.

Continue Reading »

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 »