Idiomatic Rust (for C++ Devs): Constructors & Conversions

Starting out in Rust as a C++ developer, one of the features I missed most at first were constructors. In this post, we explore the many roles that constructors have in C++ and see how they can (or can’t) be mapped to different parts of the Rust language. Although I’ll use C++ terminology, this article is likely helpful for developers coming to Rust from other languages as well.

Posted 2023-11-25
Read More

Curiously Cumbersome Rust: Type-level Programming

The moment that spawned this article was when I asked myself how hard can it be to make sure two types have the same size at compile time? Well… it’s complicated. In here, we’ll do a deep dive into the limits of compile time metaprogamming in today’s (and tonight’s) Rust.

Posted 2023-09-01 , Last Updated 2023-09-07
Read More

Learn Unsafe Rust From My Mistakes

A project of mine required me to dive into unsafe Rust and when I was done with it, I had understood something that I wanted to share. However, since I wasn’t sure if I made any subtle mistakes, I did ask the community to review my code and oh boy did it turn out that I had missed some vital things. Bear with me and hopefully you’ll gather something useful, too.

Posted 2023-07-24 , Last Updated 2023-08-07
Read More

Rust Deep Dive: Borked Vtables and Barking Cats

No, this post does not contain cruelty towards animals but only to our own sanity. We will explore a particular aspect of how Rust’s trait objects work behind the scenes and take a deep dive down the rabbit hole. Sometimes it’s good to be reminded that all the nice things we have as programmers are just sugar on top of ones and zeros in the imagination of some sand that we tricked into thinking.

Posted 2023-03-15 , Last Updated 2023-04-23
Read More

Now With RSS/Atom Feed

This blog now has a feed to which you can subscribe using the RSS logo at the top or at the bottom of the page. Or by clicking here. Updates to this blog are not frequent, but they are mostly high-effort posts and I am happy if they reach a lot of people, so if you like my stuff please subscribe :)

Posted 2023-03-02
Read More

Rust vs Common C++ Bugs

I used to like C++. I still do, but I used to, too. Joking aside, I am not one to tell any C++ programmer to just use Rust. There are a ton of valid reasons why companies or individuals decide to use C++ and I don’t wish to deter anyone from doing so. What I am trying to do in this article is to see how Rust stacks up against a handful of very common (and severe) bugs that are easy to produce in C++. I tried to make this article worthwile for both Rust and C++ programmers.

Posted 2022-12-21 , Last Updated 2022-12-26
Read More

Implementing Rayon’s Parallel Iterators - A Tutorial

The rayon library is a brilliant piece of engineering that is so simple to use that one seldom has to descend into its depths. This is the story of how I learned to implement rayon’s ParallelIterator trait for my own type. There are tons of guides on how to use rayon’s parallel iterators and there are a few explanations on how they work under the hood. However, I found no tutorials on how to implement a parallel iterator from the ground up. This article aims to close that gap.

Posted 2022-10-30 , Last Updated 2022-11-01
Read More

Compile-Time If-Expressions for Types in C++11

The other day I wondered if there is a way to metaprogam a syntax that returns a type based on a conditional expression akin to an ifelse ifelse expression. The idea came to me when I searched for a programmatic way to choose the right size of integer for a bitset, given a certain number of bits. And yes, I know there’s std::bitset, but I’ll take any excuse to investigate an interesting metaprogramming problem.

Posted 2022-08-20 , Last Updated 2023-03-24
Read More

Debugging A Stack Overflow In Rust

Recently I hit a stack overflow error in Rust and I found a helpful quick and dirty way of debugging it using a tool that a friendly member of the community hacked. If you hit a stack overflow and are too lazy to fire up your debugger, this article is for you.

Posted 2022-07-25
Read More

External Post: Raspberry Pi GPIO with Zig

A while ago I wrote an article about a small project that I did in the Zig Programming Language about controlling GPIO on the Raspberry Pi. Zig is a neat language and the whole project ended up being a whole lot of fun. You can read the article here. Although Zig is a very new – and, at the time of writing, immature– language I encourage everyone to take a look, because rather than aiming to replace C++ in all its power and complexity, it aims to replace C which makes it conceptually very different from a language like Rust.

Posted 2022-03-21
Read More

Now with Light and Dark Mode

By popular demand this site now supports both a light and a dark theme. There is no explicit way to switch between light and dark mode, instead the site will adapt to the browser preferences.

Posted 2021-10-23
Read More

C++ Metaprogramming: Safer Reading from Buffers

I am currently getting into embedded programming with C at work and while there are some things I learned to appreciate about the simplicity of C, two of the things I really miss are generics and stricter type safety. I wondered if C++ can offer a safer way of reading types from arrays of bytes. This, of course, gives me an excuse to work out my C++ template metaprogramming muscles, which have atrophied a little.

Posted 2021-10-08
Read More

Mutually Exclusive Traits in Rust

In a recent attempt to write some clever code, I set out to implement mutually exclusive traits and some blanket implementations involving these. In doing so, I hit the limits of what the compiler can do for me, but after a lot of trial-and-error, researching, and flashing the bat signal to the Rust community I had something that worked. This is how to get mutually exclusive traits working in current Rust for Future Me™ and anyone else who might be interested.

Posted 2021-07-31
Read More

Project Spotlight: varpro - Nonlinear Fitting with A Simple Interface

In the last months, I have been developing three crates for Rust. All of these crates are open source libraries that use different aspects of the language. The first crate is a numerics library, which emphasizes interfaces that are powerful but simple to use. The second one is a fluent coding library using mostly declarative macros and the third crate uses procedural macros to help with expressive assertions. For my next series of posts I am going to introduce these three crates and highlight a particular aspect of each one. First up is the varpro library.

Posted 2021-05-27
Read More

Rust: A Common Interface for Functions Taking Different Numbers of Arguments

I am working on a little numerics project where the goal is to let the user pass base functions to a data-fitting model in a simple fashion. And while I really like what Rust’s lambdas can already do for me, I was wondering if I can pass functions with different (but compile time constant) numbers of parameters using a single interface. It turns out that I can, and the answer is a nice combination of Rust and C++ metaprogramming idioms.

Posted 2021-02-15
Read More

Understanding the DFT: Spectral Leakage, Windowing, and Periodicity

I was going to write something about how windowing could be used to mitigate the effects of spectral leakage in the DFT. However, I went down a rabbit hole trying to truly understand where spectral leakage comes from and how it relates to the periodicity of the DFT. What I ended up writing, is a post that is more about understanding the DFT and less about windowing, although it plays an important role in this post.

Posted 2021-01-17
Read More

Back to Basics: The (Discrete) Fourier Transform

I have extensively worked with the Fourier transform and the DFT but never really taken the time to understand how these two things are truly connected and how that connection explains some of the properties of the DFT. In this article I explore this connection with the aim of answering one question: what does the DFT actually compute?

Posted 2020-12-30
Read More

Mutexes in Rust and C++: Protecting Data versus Protecting Code

I have been using Rust in production for a couple of months now and there is lots about it that I like. One neat thing is that Rust’s compile time guarantees make memory and thread safety much harder to mess up from the get go. This allows Rust programmers to confidently use some paradigms that are harder to use correctly in C++. One such example is protecting shared data with mutexes, which works very differently in Rust than in C++. This post is about exploring those fundamental differences.

Posted 2020-12-01
Read More

Replacing the Ternary Operator with Rust-style If Expressions in C++11 - Part 1

Right now I am getting into Rust, which lets me see the paradigms of C++ in a new light. This is a small thing, but for example: C++ has if statements and Rust has if expressions which can return a value. This inspired me to implement an expressive syntax for mimicking Rust’s if expressions in C++. To keep things simple, I only targeted the use case of replacing nested ternary operators.

Posted 2020-07-06
Read More

Implementing a Pipe Syntax for std::optional - Part 3: Indended Side Effects and Unintended Limitations

This is the last part of my series on a pipe syntax for operations on std::optional types. We will look at switching between function overloads at compile time using std::enable_if to implement the last puzzle piece we were missing before. I will also show some limitations of my implementation and describe how my pipe operator fits in to the overarching idea of functional programming.

Posted 2020-06-07
Read More

The Variable Projection Method - Nonlinear Least Squares Fitting with VarPro (Update 12/2023)

The Variable Projection method is a lesser known algorithm in the domain of nonlinear least squares fitting. It is interesting because it makes clever use of linear algebra to potentially speed up fitting certain classes of functions to data. I’ll introduce the method such a way that it will enable you to implement your own varpro library in your favorite programming language. Only a basic understanding of linear algebra and calculus is required.

Posted 2020-05-24 , Last Updated 2023-12-22
Read More

Bayesian Analysis of a Game for Children - Part 2: The Results And The Twist

These are the results of my foray into Bayesian data analysis applied to a childrens game. This post ties the previous posts concerning simple games, probability theory and functional programming together. After recapping the rules of the particular game I will present the winningest strategy, which will surprise nobody. But I will finish with a plot twist.

Posted 2020-05-15
Read More

Immutability in C++ (1/2): Immutable Members

In an attempt to increase my functional programming skills in C++, I went full functional when writing a simulation for a childrens game. Trying to implement a game state using the paradigms of functional programming presents some interesting challenges. In this post I will explore the concept of immutability and how we can implement it in C++ for all kinds of stateful objects.

Posted 2020-03-10
Read More

Now with Comments

This blog now has a comment functionality. I can switch on comments for individual posts and everyone with a GitHub account is able to comment.

Posted 2020-02-16
Read More

Bayesian Analysis of a Game for Children - Part 1: Fundamentals

While playing a boardgame for three-year-olds with my daughter, my mind started to wander. Boardgames for young children give the player very little agency to influence the game, if any. Yet I wondered what the best playing strategy for this particular game was. The game is simple enough that I can start writing a simulation but now I am faced with the problem of how to interpret the data. How confident can I be that one strategy really is better than the other? Deriving these answers from scratch is the topic of this series.

Posted 2020-02-08
Read More

Welcome

Welcome to my blog in which I aim to explore some of my personal and professional favorites: Programming, Math and the occasional ramblings.

Posted 2020-01-07
Read More