Rust: How non-primitive types are available in the default scope

In rust, str is a primitive type, but many non-primitive types are also in scope by default.

e.g. We do not need to add use statement to use Vec - which is NOT a primitive type.

It comes from std::vec

So Vec::new() is really std::vec::Vec::new()

Vec::new() works because Rust inserts this at the beginning of every module:

use std::prelude::v1::*;

This makes Vec (and String, Option and Result) available by default.

Continue Reading »

How Zellij saved me

OK, title is a bit of clickbait. It saved me some inconvinence ๐Ÿ˜‰

Yesterday, Wezterm crashed.

At first, I was worried that all my “work” is lost (few blog posts were in-progress and open in helix)

But I had forgotten that I always start zellij these days.

So all my “work” was indeed there.

All I had to do was zellij ls followed by zellij a my_session1 (and my_session2 and so on)

Continue Reading »

Importance of Making Things

On the knowledge project podcast, Shane parrish interviewed Kevin Kelly

The link above has good high level overview with timestamps. Definitely check it out.

One thing that is not covered in the timestamps (hence I can’t link to the exact conversation) is about Importance of making things 1

Kevin says something to the effect of, don’t stop making things (that you care about) just because they are not the greatest or the best. Your “thing” may have influence (or use) for someone in the future in ways you can’t imagine.

Continue Reading »

Homework for Life

In One percent better podcast 1, Joe interviews Matthew Dicks

Matthew explains the Homework for Life 2 as (paraphrased)

At the end of each day, write down important “stories” (not memories) of the day. Else life passes by, and we don’t remember what you did 12 years from today. (Cause all days look the same in past)

He is a teacher. He asks his students to narrate stories (on a Monday, start of the school) He mentions that 10-year olds can come up with excellent stories. One example he mentions is

Continue Reading »

Vivaldi Browser

I switched to 1 Vivaldi browser after reading this post by Mike Kennedy

To be fair, I had installed and tried Vivaldi a long time ago. But I think things got better this time around.

I seem to have stuck with it for long time.

One benefit of chromium based browsers (like Vivaldi) is that user has access to vast amount of extensions from Chrome Web store. 2

Just today, I explored two other “features” which I’m not sure how I feel about.

Continue Reading »

`#[cfg]` attrib vs `cfg!` macro in Rust

When I published my previous post on mastodon, Sebastian pointed out 1 that using #[cfg] is better than cfg! macro.

Documentation explains that :

cfg!, unlike #[cfg], does not remove any code and only evaluates to true or false. For example, all blocks in an if/else expression need to be valid when cfg! is used for the condition, regardless of what cfg! is evaluating.

Thanks, Sebastian!


  1. See this thread for the original discussion. ↩︎

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 »

You can now follow this blog from Fediverse

Put @microblog.desipenguin.com@web.brid.gy in the search box.

Thanks to Bridgy.fed this site automatically gets it’s own mastodon account.

Following this account is like subscribing to the RSS feed (I think ๐Ÿ˜„)