<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>rustlang &amp;mdash; veer66</title>
    <link>https://qua.name/veer66/tag:rustlang</link>
    <description>veer66</description>
    <pubDate>Sat, 18 Apr 2026 17:30:31 +0200</pubDate>
    <item>
      <title>I try different formats using Rust&#39;s Serde</title>
      <link>https://qua.name/veer66/i-try-different-formats-using-rusts-serde</link>
      <description>&lt;![CDATA[I try different to serialize my data before saving into RocksDB. #rustlang&#xA;&#xA;serdejson&#xA;&#xA;Time &#xA;&#xA;real    4m11.713s&#xA;user    13m32.809s&#xA;sys     1m33.887s&#xA;&#xA;Space&#xA;&#xA;2.1GB&#xA;&#xA;bincode&#xA;&#xA;Time&#xA;&#xA;real    2m13.772s&#xA;user    10m45.541s&#xA;sys     1m41.670s&#xA;&#xA;Space&#xA;&#xA;1.1GB&#xA;&#xA;serde-lexpr (S-Expression)&#xA;&#xA;Time&#xA;&#xA;real    10m54.622s&#xA;user    22m11.570s&#xA;sys     1m10.663s&#xA;&#xA;Space&#xA;&#xA;2.2GB&#xA;&#xA;serdecbor&#xA;&#xA;Time&#xA;&#xA;real    2m52.010s&#xA;user    12m17.019s&#xA;sys     1m32.687s&#xA;&#xA;Space&#xA;&#xA;1.7GB&#xA;&#xA;CBOR and bincode are obviously faster than JSON and S-Expression. CBOR is a less efficient than bincode. However, more programming languages support CBOR.]]&gt;</description>
      <content:encoded><![CDATA[<p>I try different to serialize my data before saving into RocksDB. <a href="/veer66/tag:rustlang" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">rustlang</span></a></p>

<h1 id="serde-json" id="serde-json">serde_json</h1>

<h2 id="time" id="time">Time</h2>

<pre><code>real    4m11.713s
user    13m32.809s
sys     1m33.887s
</code></pre>

<h2 id="space" id="space">Space</h2>

<p>2.1GB</p>

<h1 id="bincode" id="bincode">bincode</h1>

<h2 id="time-1" id="time-1">Time</h2>

<pre><code>real    2m13.772s
user    10m45.541s
sys     1m41.670s
</code></pre>

<h2 id="space-1" id="space-1">Space</h2>

<p>1.1GB</p>

<h1 id="serde-lexpr-s-expression" id="serde-lexpr-s-expression">serde-lexpr (S-Expression)</h1>

<h2 id="time-2" id="time-2">Time</h2>

<pre><code>real    10m54.622s
user    22m11.570s
sys     1m10.663s
</code></pre>

<h2 id="space-2" id="space-2">Space</h2>

<p>2.2GB</p>

<h1 id="serde-cbor" id="serde-cbor">serde_cbor</h1>

<h2 id="time-3" id="time-3">Time</h2>

<pre><code>real    2m52.010s
user    12m17.019s
sys     1m32.687s
</code></pre>

<h2 id="space-3" id="space-3">Space</h2>

<p>1.7GB</p>

<p>CBOR and bincode are obviously faster than JSON and S-Expression. CBOR is a less efficient than bincode. However, more programming languages support CBOR.</p>
]]></content:encoded>
      <guid>https://qua.name/veer66/i-try-different-formats-using-rusts-serde</guid>
      <pubDate>Fri, 04 Jun 2021 10:21:43 +0200</pubDate>
    </item>
    <item>
      <title> The prevention of flipping ids </title>
      <link>https://qua.name/veer66/the-prevention-of-flipping-ids</link>
      <description>&lt;![CDATA[Given I have a file of document id and text unit id. For example:&#xA;&#xA;doc-id, text-unit-ids&#xA;1,100,200,300&#xA;2,50,8,1,6&#xA;&#xA;I want to keep them in a list of doc-id, text-unit-id pairs. For example:&#xA;&#xA;1,100&#xA;1,200&#xA;1,300&#xA;2,50&#xA;2,8&#xA;2,1&#xA;2,6&#xA;&#xA;So I defined my variable in Rust, as follow:&#xA;&#xA;let wordidtuids: Vec(u32, u32) = vec![];&#xA;&#xA;Sometimes I have a problem that I flipped them. I put a pair of text-unit-id and doc-id instead of text-unit-it and doc-id. For example, I put 100,1 instead of 100,1. &#xA;&#xA;By (u32, u32), the Rust compiler cannot help me. So I tried:&#xA;&#xA;type WordId = u32;&#xA;type DocId = u32;&#xA;&#xA;fn push(w: WordId, d: DocId) {&#xA;    let mut v: Vec(WordId, DocId) = vec![];&#xA;    v.push((d, w));&#xA;}&#xA;&#xA;It didn&#39;t help. So I tried struct instead. &#xA;&#xA;struct WordId(u32);&#xA;struct DocId(u32);&#xA;&#xA;fn push(w: WordId, d: DocId) {&#xA;    let mut v: Vec(WordId, DocId) = vec![];&#xA;    v.push((d, w));&#xA;}&#xA;&#xA;Now the compiler can detect the error. However, maybe it is not clear to human eyes. So I defined another struct.&#xA;&#xA;struct WordId(u32);&#xA;struct DocId(u32);&#xA;&#xA;struct WordIdDocId {&#xA;    wordid: WordId,&#xA;    docid: DocId,&#xA;}&#xA;&#xA;fn push(w: WordId, d: DocId) {&#xA;    let mut v: VecWordIdDocId = vec![];&#xA;    v.push(WordIdDocId {wordid: w, docid: d});&#xA;}&#xA;&#xA;Now it is clear to human eyes and the compiler. Anyways, is it what people call over-engineering? What if I want to: &#xA;&#xA;let u = w + 1;&#xA;&#xA;We can do it in Rust, but the code is going to be even longer.&#xA;published: true&#xA;description: &#xA;tags: #rust #rustlang&#xA;//coverimage: https://directurltoimage.jpg&#xA;---&#xA;&#xA;Given I have a file of document id and text unit id. For example:&#xA;&#xA;doc-id, text-unit-ids&#xA;1,100,200,300&#xA;2,50,8,1,6&#xA;&#xA;I want to keep them in a list of doc-id, text-unit-id pairs. For example:&#xA;&#xA;1,100&#xA;1,200&#xA;1,300&#xA;2,50&#xA;2,8&#xA;2,1&#xA;2,6&#xA;&#xA;So I defined my variable in Rust, as follow:&#xA;&#xA;let wordidtuids: Vec(u32, u32) = vec![];&#xA;&#xA;Sometimes I have a problem that I flipped them. I put a pair of text-unit-id and doc-id instead of text-unit-it and doc-id. For example, I put 100,1 instead of 100,1. &#xA;&#xA;By (u32, u32), the Rust compiler cannot help me. So I tried:&#xA;&#xA;type WordId = u32;&#xA;type DocId = u32;&#xA;&#xA;fn push(w: WordId, d: DocId) {&#xA;    let mut v: Vec(WordId, DocId) = vec![];&#xA;    v.push((d, w));&#xA;}&#xA;&#xA;It didn&#39;t help. So I tried struct instead. &#xA;&#xA;struct WordId(u32);&#xA;struct DocId(u32);&#xA;&#xA;fn push(w: WordId, d: DocId) {&#xA;    let mut v: Vec(WordId, DocId) = vec![];&#xA;    v.push((d, w));&#xA;}&#xA;&#xA;Now the compiler can detect the error. However, maybe it is not clear to human eyes. So I defined another struct.&#xA;&#xA;struct WordId(u32);&#xA;struct DocId(u32);&#xA;&#xA;struct WordIdDocId {&#xA;    wordid: WordId,&#xA;    docid: DocId,&#xA;}&#xA;&#xA;fn push(w: WordId, d: DocId) {&#xA;    let mut v: VecWordIdDocId = vec![];&#xA;    v.push(WordIdDocId {wordid: w, docid: d});&#xA;}&#xA;&#xA;Now it is clear to human eyes and the compiler. Anyways, is it what people call over-engineering? What if I want to: &#xA;&#xA;let u = w + 1;&#xA;&#xA;We can do it in Rust, but the code is going to be even longer.]]&gt;</description>
      <content:encoded><![CDATA[<p>Given I have a file of document id and text unit id. For example:</p>

<pre><code class="language-cvs">doc-id, text-unit-ids
1,100,200,300
2,50,8,1,6
</code></pre>

<p>I want to keep them in a list of doc-id, text-unit-id pairs. For example:</p>

<pre><code>1,100
1,200
1,300
2,50
2,8
2,1
2,6
</code></pre>

<p>So I defined my variable in Rust, as follow:</p>

<pre><code class="language-Rust">let word_id_tu_ids: Vec&lt;(u32, u32)&gt; = vec![];
</code></pre>

<p>Sometimes I have a problem that I flipped them. I put a pair of text-unit-id and doc-id instead of text-unit-it and doc-id. For example, I put 100,1 instead of 100,1.</p>

<p>By (u32, u32), the Rust compiler cannot help me. So I tried:</p>

<pre><code class="language-Rust">type WordId = u32;
type DocId = u32;

fn push(w: WordId, d: DocId) {
    let mut v: Vec&lt;(WordId, DocId)&gt; = vec![];
    v.push((d, w));
}
</code></pre>

<p>It didn&#39;t help. So I tried struct instead.</p>

<pre><code class="language-Rust">struct WordId(u32);
struct DocId(u32);

fn push(w: WordId, d: DocId) {
    let mut v: Vec&lt;(WordId, DocId)&gt; = vec![];
    v.push((d, w));
}
</code></pre>

<p>Now the compiler can detect the error. However, maybe it is not clear to human eyes. So I defined another struct.</p>

<pre><code class="language-Rust">struct WordId(u32);
struct DocId(u32);

struct WordIdDocId {
    word_id: WordId,
    doc_id: DocId,
}

fn push(w: WordId, d: DocId) {
    let mut v: Vec&lt;WordIdDocId&gt; = vec![];
    v.push(WordIdDocId {word_id: w, doc_id: d});
}
</code></pre>

<p>Now it is clear to human eyes and the compiler. Anyways, is it what people call over-engineering? What if I want to:</p>

<pre><code class="language-Rust">let u = w + 1;
</code></pre>

<p>We can do it in Rust, but the code is going to be even longer.
published: true
description:
tags: <a href="/veer66/tag:rust" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">rust</span></a> <a href="/veer66/tag:rustlang" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">rustlang</span></a>
//cover<em>image: <a href="https://direct" rel="nofollow">https://direct</a></em>url<em>to</em>image.jpg</p>

<hr>

<p>Given I have a file of document id and text unit id. For example:</p>

<pre><code class="language-cvs">doc-id, text-unit-ids
1,100,200,300
2,50,8,1,6
</code></pre>

<p>I want to keep them in a list of doc-id, text-unit-id pairs. For example:</p>

<pre><code>1,100
1,200
1,300
2,50
2,8
2,1
2,6
</code></pre>

<p>So I defined my variable in Rust, as follow:</p>

<pre><code class="language-Rust">let word_id_tu_ids: Vec&lt;(u32, u32)&gt; = vec![];
</code></pre>

<p>Sometimes I have a problem that I flipped them. I put a pair of text-unit-id and doc-id instead of text-unit-it and doc-id. For example, I put 100,1 instead of 100,1.</p>

<p>By (u32, u32), the Rust compiler cannot help me. So I tried:</p>

<pre><code class="language-Rust">type WordId = u32;
type DocId = u32;

fn push(w: WordId, d: DocId) {
    let mut v: Vec&lt;(WordId, DocId)&gt; = vec![];
    v.push((d, w));
}
</code></pre>

<p>It didn&#39;t help. So I tried struct instead.</p>

<pre><code class="language-Rust">struct WordId(u32);
struct DocId(u32);

fn push(w: WordId, d: DocId) {
    let mut v: Vec&lt;(WordId, DocId)&gt; = vec![];
    v.push((d, w));
}
</code></pre>

<p>Now the compiler can detect the error. However, maybe it is not clear to human eyes. So I defined another struct.</p>

<pre><code class="language-Rust">struct WordId(u32);
struct DocId(u32);

struct WordIdDocId {
    word_id: WordId,
    doc_id: DocId,
}

fn push(w: WordId, d: DocId) {
    let mut v: Vec&lt;WordIdDocId&gt; = vec![];
    v.push(WordIdDocId {word_id: w, doc_id: d});
}
</code></pre>

<p>Now it is clear to human eyes and the compiler. Anyways, is it what people call over-engineering? What if I want to:</p>

<pre><code class="language-Rust">let u = w + 1;
</code></pre>

<p>We can do it in Rust, but the code is going to be even longer.</p>
]]></content:encoded>
      <guid>https://qua.name/veer66/the-prevention-of-flipping-ids</guid>
      <pubDate>Fri, 04 Jun 2021 10:18:00 +0200</pubDate>
    </item>
    <item>
      <title>เรื่องใส่ id สลับที่กัน </title>
      <link>https://qua.name/veer66/eruue-ngais-id-slabthiikan</link>
      <description>&lt;![CDATA[เขียน #rustlang แบบ &#xA;&#xA;(wordid, textunitid) &#xA;&#xA;วันเดียวพอจำได้หรอก เขียนไปนาน ๆ อาจจะพลาดใส่ &#xA;&#xA;(textunitid, wordid)&#xA;&#xA;ก็ได้&#xA;&#xA;อันนี้ static type อาจจะวืดเพราะทั้ง textunit-id และ word-id ก็เป็น u32 ทั้งคู่ อาจจะแก้แบบนี้สร้าง type มาใหม่เลย WordId กับ TextunitId แต่มันก็เหนื่อยอยู่นะ&#xA;&#xA;ไม่ก็ใช้ struct แทน เช่น&#xA;&#xA;struct WordIdTextunitId {&#xA;    wordid: u32,&#xA;    textunitid: u32,&#xA;}&#xA;&#xA;อันนี้เขียนง่ายดี แต่ compiler ไม่ได้ช่วยอะไรมากนะ ก็คือ programmer ก็ดูเอาเมื่อไหร่เขียน&#xA;&#xA;WordIdTextunitId { wordid: textunitid, textunitid: wordid }&#xA;&#xA;แบบนี้ก็ผิด โปรแกรมเมอร์เห็นเอง แต่ใส่สลับกัน compiler มันก็ผ่านนะ&#xA;&#xA;แล้วคิดไปติดมามันก็พอกับเขียน Clojure เลย&#xA;&#xA;{:word-id word-id&#xA; :textunit-id textunit-id}&#xA;แบบนี้ก็ชัดเจนอยู่แล้ว ไม่ต้องประกาศ struct ด้วย หรือจะใช้ defstruct เลยก็ได้&#xA;&#xA;ใน Common Lisp ก็คล้าย ๆ กัน def struct ก็ได้ จะทำเป็น alist ก็ได้&#xA;&#xA;(list (cons :word-id word-id) &#xA;       (cons :textunit-id textunit-id))&#xA;&#xA;แบบนี้ก็ได้&#xA;&#xA;กลับมา Rust จะจัดหนักแบบ ทำแบบนี้ก็บึ้มอยู่ดี&#xA;&#xA;type WordId = u32;&#xA;type TextunitId = u32;&#xA;&#xA;struct WordIdTextUnitId {&#xA;    wordid: WordId,&#xA;    textunitid: TextunitId,&#xA;}&#xA;&#xA;fn main() {&#xA;   let w: WordId = 1;&#xA;   let t: TextunitId = 2;&#xA;   let x = WordIdTextUnitId { wordid: t, textunitid: w};&#xA;}&#xA;&#xA;สลับได้ compiler ไม่ช่วยอะไร&#xA;&#xA;แต่ถ้าทำเป็น struct หมดเลย&#xA;&#xA;struct WordId(u32);&#xA;struct TextunitId(u32);&#xA;&#xA;struct WordIdTextUnitId {&#xA;    wordid: WordId,&#xA;    textunitid: TextunitId,&#xA;}&#xA;&#xA;fn main() {&#xA;    let w = WordId(1);&#xA;    let t = TextunitId(2);&#xA;    let x = WordIdTextUnitId { wordid: t, textunitid: w};&#xA;}&#xA;&#xA;แบบนี้ compiler มันจะช่วยได้รู้แล้วว่าสลับกัน&#xA;&#xA;แต่ก็จะมาเจอว่าผมอยากได้ &#xA;&#xA;let v = w + 1; &#xA;&#xA;แบบนี้ทำไง ก็มีวิธีทำหลายทางนะ เพียงแต่มันก็ต้องออกแรง]]&gt;</description>
      <content:encoded><![CDATA[<p>เขียน <a href="/veer66/tag:rustlang" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">rustlang</span></a> แบบ</p>

<pre><code class="language-Rust">(word_id, textunit_id) 
</code></pre>

<p>วันเดียวพอจำได้หรอก เขียนไปนาน ๆ อาจจะพลาดใส่</p>

<pre><code class="language-Rust">(textunit_id, word_id)
</code></pre>

<p>ก็ได้</p>

<p>อันนี้ static type อาจจะวืดเพราะทั้ง textunit-id และ word-id ก็เป็น u32 ทั้งคู่ อาจจะแก้แบบนี้สร้าง type มาใหม่เลย WordId กับ TextunitId แต่มันก็เหนื่อยอยู่นะ</p>

<p>ไม่ก็ใช้ struct แทน เช่น</p>

<pre><code class="language-Rust">struct WordIdTextunitId {
    word_id: u32,
    textunit_id: u32,
}
</code></pre>

<p>อันนี้เขียนง่ายดี แต่ compiler ไม่ได้ช่วยอะไรมากนะ ก็คือ programmer ก็ดูเอาเมื่อไหร่เขียน</p>

<pre><code class="language-Rust">WordIdTextunitId { word_id: textunit_id, textunit_id: word_id }
</code></pre>

<p>แบบนี้ก็ผิด โปรแกรมเมอร์เห็นเอง แต่ใส่สลับกัน compiler มันก็ผ่านนะ</p>

<p>แล้วคิดไปติดมามันก็พอกับเขียน Clojure เลย</p>

<pre><code class="language-Clojure">{:word-id word-id
 :textunit-id textunit-id}
</code></pre>

<p>แบบนี้ก็ชัดเจนอยู่แล้ว ไม่ต้องประกาศ struct ด้วย หรือจะใช้ defstruct เลยก็ได้</p>

<p>ใน Common Lisp ก็คล้าย ๆ กัน def struct ก็ได้ จะทำเป็น alist ก็ได้</p>

<pre><code class="language-Lisp">(list (cons :word-id word-id) 
       (cons :textunit-id textunit-id))
</code></pre>

<p>แบบนี้ก็ได้</p>

<p>กลับมา Rust จะจัดหนักแบบ ทำแบบนี้ก็บึ้มอยู่ดี</p>

<pre><code class="language-Rust">type WordId = u32;
type TextunitId = u32;

struct WordIdTextUnitId {
    word_id: WordId,
    textunit_id: TextunitId,
}

fn main() {
   let w: WordId = 1;
   let t: TextunitId = 2;
   let x = WordIdTextUnitId { word_id: t, textunit_id: w};
}
</code></pre>

<p>สลับได้ compiler ไม่ช่วยอะไร</p>

<p>แต่ถ้าทำเป็น struct หมดเลย</p>

<pre><code class="language-Rust">struct WordId(u32);
struct TextunitId(u32);

struct WordIdTextUnitId {
    word_id: WordId,
    textunit_id: TextunitId,
}

fn main() {
    let w = WordId(1);
    let t = TextunitId(2);
    let x = WordIdTextUnitId { word_id: t, textunit_id: w};
}
</code></pre>

<p>แบบนี้ compiler มันจะช่วยได้รู้แล้วว่าสลับกัน</p>

<p>แต่ก็จะมาเจอว่าผมอยากได้</p>

<pre><code class="language-Rust">let v = w + 1; 
</code></pre>

<p>แบบนี้ทำไง ก็มีวิธีทำหลายทางนะ เพียงแต่มันก็ต้องออกแรง</p>
]]></content:encoded>
      <guid>https://qua.name/veer66/eruue-ngais-id-slabthiikan</guid>
      <pubDate>Thu, 03 Dec 2020 06:21:59 +0100</pubDate>
    </item>
  </channel>
</rss>