Skip to content

Rust

Rust as_ref and as_mut Usage in the Linked List

Recently, I tried to write leetcode by rust. Everything goes well until I started to write code for linked list. The borrow check is quite verbose and I got stuck by the borrow check when handling Option<Box<LinkedNode>> more than the problem itself.

It's sad to find I don't understand the borrow check well, but it's also a good chance to find these missing knowledge for me.

This blog discusses about as_ref, as_mut and take of the Option.

Insert Code to Leetcode Generated Code

When I wrote leetcode in vscode with the plugin, I named generated leetcode file with format as[0-9]+(e.g, as123.rs) and put them under src. However, I have encountered some painpoints:

  1. The generated file isn't used by main.rs so the rust analyzer will ignore it, so I need to add mod as123; in main.rs every time.
  2. The generated code doesn't contain struct Soltuion definition so I need to add it every time.
  3. When I want to debug locally, I need to write the test function from scratch. It doesn't cost much but it's verbose.

These painpoints are trivial, but they're quite verbose. By using build script build.rs, I'm succeeded solving my painpoints!

Make Bazel Work in MacOS for Iceoryx2

In iceoryx2, it uses bazel to build C/C++ bindings. Hence, they want to use bazel for rust for a better unification in iceoryx2.

Currently foreign_cc is used to build the C binding and cc_library to build the C++ bindings. In order to fully exhaust the capabilities of bazel, the C binding and therefore the Rust code should be build with rules_rust.

When I tried it, I found it doesn't work in my MacOS. This blog records some knowledge when I learned bazel and how I solved the problem. It doesn't talk about why we use bazel in a rust project.

Iceoryx2 Lockfree Structures Benchmark

This page lists the benchmark results of the iceoryx2 lockfree structures. At the beginning, I hold the assumption that the lockfree structures are faster. However, that's not a true assumption. There is no significant differences between locked implementation and lockfree implementation in my test cases.

You can view my benchmark code here.