Rust

ABI - Use Rust code from C

I just followed “The Rust Reference: 17. Application Binary Interface (ABI)” and “The Embedded Rust Book: 10.2 A little Rust with your C” After then, I tried to expose Rust program to C as ABI. Test the code in the first reference foo.rs: // This is kept because of `#[used]`: #[used] static FOO: u32 = 123; // This is removable because it is unused: #[allow(dead_code)] static BAR: u32 = 0; // This is kept because it is publicly reachable: pub static BAZ: u32 = 0; // This is kept because it is referenced by a public, reachable function: static QUUX: u32 = 0; pub fn quux() -> &'static u32 { &QUUX } // This is removable because it is referenced by a private, unused (dead) function: static CORGE: u32 = 0; #[allow(dead_code)] fn corge() -> &'static u32 { &CORGE } used attribute The attribute can only be applied to static item.

Memory allocation in Rust

While learning “15.3. Running Code on Cleanup with the Drop Trait” of the Rust official book, I just wondered the memory layout of Rust program. TL;DR: Currently the default global allocator is unspecified. Libraries, however, like cdylibs and staticlibs are guaranteed to use the System by default. Struct std::alloc::System: The default memory allocator provided by the operating system. This is based on malloc on Unix platforms and HeapAlloc on Windows, plus related functions.

Linear Algebra libraries for Rust

I tried to implement LWE with Rust. Here is the memo about linear algebra libraries. Which library to use As of Nov.2021, there was no “defacto standard” linear algebra library in Rust. I investigated, and decided to use ndarray or nlagebra. ndarray vs nalgebra - Reddit ndarray https://github.com/rust-ndarray/ndarray tensorflow/Rust uses ndarray. ndarray is mentioned in Rust cookbook: ndarray use BLAS impelementation on the runtime (Intel MKL, OpenBLAS, etc.), so it could be faster than other libraries.

Serde - tutorial

Serde (serializing and deserializing) is one of the most frequently used library in Rust. The library is production ready, usually used with Serde JSON (because of the popularity of REST APIs). Serialization From Wikipedia, In computing, serialization is the process of translating a data structure or object state into a format that can be stored (for example, in a file or memory data buffer) or transmitted (for example, over a computer network) and reconstructed later (possibly in a different computer environment)

Newtype Pattern, Avoid String Parameter in Strongly-Typed Languages

Why? String type has generally lots of choices for the values, so it prones to bugs. If your programing language supports strong-typing, you need to consider avoiding Strings as parameters of functions. Strong-typing led you Less bugs and validations. In Rust, it’s called a “newtype pattern”. The trinity, “Good naming (based on DDD) + good typing (struct) + enum”, helps us always. Newtype patterns in Rust https://doc.rust-lang.org/book/ch19-03-advanced-traits.html use std::ops::Add; struct Millimeters(u32); struct Meters(u32); impl Add<Meters> for Millimeters { type Output = Millimeters; fn add(self, other: Meters) -> Millimeters { Millimeters(self.

Basic sort algorithms in Rust

Disclamer The implementations in this post have a lot of room for improvement. As of Apr. 2021, they are more like code monkey’s code :( Reverse a string fn main() { let s1 = String::from("Please reverse me with spaces!日本語.한국어"); println!("Original string: {}",s1); let mut s2 = String::from(""); for c in s1.chars().rev() { s2.push(c); } println!("Reversed: {}",s2); } In Rust, both String and a string slice &str are UTF-8 encoded. chars() returns an iterator over chars of a string slice.

Rust Serverless (2021)

CAUTION (2022) The first virsion of this post was created in Apri 2021, but as of 2022, AWS officially released their SDK. This post (note) should be updated. Still there is no native run environment for Rust on AWS Lambda as of Jan.18.2022, so we still need to create a bootstrap executable. Getting started I followed the link. In this page, the author wrote details and it’s very educational. To remind the procedure, I left memo.

WASM by Rust - Tutorial

Tutorial The URL I followed. https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm Pre-requirement You should install npm beforehand. To compile wasm-pack, apt install -y build-essential and install gcc. In case of Ubuntu, apt install -y libssl-dev pkg-config. Download wasm-pack To build the package, we need an additional tool, wasm-pack. This helps compile the code to WebAssembly, as well as produce the right packaging for npm. cargo install wasm-pack Write codes cargo new --lib hello-wasm cd hello-wasm src/lib.

Install Rust on various platform

Official document. WSL (Nov, 2020) In my environment (Windows Subsystem Linux), I could install as follows. curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 1) Proceed with installation (default) 2) Customize installation 3) Cancel installation >1 echo "export PATH=$HOME/.cargo/bin:$PATH " >> ~/.bashrc CentOS8 CentOS 8 in GCP. Basically same, but just for a memo. curl -sSf https://sh.rustup.rs | sh When memory allocation failed, try to set up RUSTUP_UMPACK_RAM. https://github.com/rust-lang/rustup/issues/2128 Update Rust If you install Rust in this way, you can update your Rust by the command below: