<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Rust on aliquote.org</title><link>https://aliquote.org/tags/rust/</link><description>Recent content in Rust on aliquote.org</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Wed, 20 May 2026 10:03:01 +0200</lastBuildDate><atom:link href="https://aliquote.org/tags/rust/index.xml" rel="self" type="application/rss+xml"/><item><title>Linear regression in Rust</title><link>https://aliquote.org/post/linear-regression-rust/</link><pubDate>Wed, 20 May 2026 10:03:01 +0200</pubDate><guid>https://aliquote.org/post/linear-regression-rust/</guid><description>&lt;div class="alert alert-note"&gt;
&lt;small&gt;[2026-07-17]&lt;/small&gt;&lt;br&gt;
This is a short note I wrote two months ago when I was studying the design of
the [ndarray][1] Rust library. I intended to complete the project with some
tests and output some plot using any Gnuplot crate I would find but I lost
interest in this project in the mean time. I guess this is just for posterity
now. However, it was a fun exercise and I really enjoyed malaxing some Rust code
for a statistical toy project.
&lt;/div&gt;
&lt;p&gt;Lately, I&amp;rsquo;ve been playing with some Rust. I found a toy example of linear
regression in Rust: &lt;a href="http://lorenzocelli.me/2025/06/28/linear-regression-in-Rust.html"&gt;Linear Regression in Rust&lt;/a&gt;. Be sure to check the
associated GitHub repository as the sample code from the blog post isn&amp;rsquo;t
expanded to its full length. This really was a quick check to see what&amp;rsquo;s
available in Rust, and how developing in Rust feels when using Neovim. About the
second point, I want to be able to run tests directly from within my editor, as
well as launch a debugger without relying on external task file or firing up
another terminal. Note that I removed uninteresting part of the original code
(linear regression implemented using standard formulae based on summated
standard deviations).&lt;/p&gt;
&lt;p&gt;I got several error with &lt;code&gt;cargo run&lt;/code&gt; due to openblas-build requiring the
&lt;code&gt;rustls&lt;/code&gt; or &lt;code&gt;native-tls&lt;/code&gt; feature to be enabled. (I&amp;rsquo;m on macOS Tahoe, running on
an ARM64 machine.) Rather than struggling with building a static lib for
openblas, I wanted to use native macOS BLAS and LAPACK libraries. To use macOS
&lt;a href="https://developer.apple.com/accelerate/"&gt;Accelerate&lt;/a&gt; framework instead of openblas, we must change the spec file
(&lt;code&gt;Cargo.toml&lt;/code&gt;) so that it looks like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-toml" data-lang="toml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;csv&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;1.1&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ndarray&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;0.15&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;features&lt;/span&gt;&lt;span class="p"&gt;=[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;rayon&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;serde&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;blas&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ndarray-linalg&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;0.16&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;default-features&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;blas-src&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;version&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;0.8&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;features&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;accelerate&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We also need to add specific instructions for linking the target in &lt;code&gt;build.rs&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;cargo:rustc-link-lib=framework=Accelerate&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is discussed in the following &lt;a href="https://github.com/rust-ndarray/ndarray-linalg/issues/362#issuecomment-1650731137"&gt;GH issue&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Running the original code as is, everything works out of the box:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-shell" data-lang="shell"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -%&amp;lt;--
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Compiling ndarray-linalg v0.16.0
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Finished &lt;span class="sb"&gt;`&lt;/span&gt;dev&lt;span class="sb"&gt;`&lt;/span&gt; profile &lt;span class="o"&gt;[&lt;/span&gt;unoptimized + debuginfo&lt;span class="o"&gt;]&lt;/span&gt; target&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; in 14.93s
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Running &lt;span class="sb"&gt;`&lt;/span&gt;target/debug/regress&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;[&lt;/span&gt;2.9388893694493694, 0.04576464545542497, 0.188530016918319, -0.0010374930424147009&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;RSE: 1.6769760888385676
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Full code available on GH in this &lt;a href="https://github.com/even4void/scratch/tree/master/regress"&gt;subfolder&lt;/a&gt;, with all credits to Lorenzo
Celli for the original code.&lt;/p&gt;
&lt;div class="music" align="right"&gt;
&lt;p&gt;♪ A Flock of Seagulls • &lt;em&gt;Over My Head&lt;/em&gt;&lt;/p&gt;
&lt;/div&gt;</description></item></channel></rss>