<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Musab Khan</title>
    <description>The latest articles on DEV Community by Musab Khan (@musab0986).</description>
    <link>https://dev.to/musab0986</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3960967%2Fb9eeb8ed-a803-4812-b01c-469bbe87ac87.png</url>
      <title>DEV Community: Musab Khan</title>
      <link>https://dev.to/musab0986</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/musab0986"/>
    <language>en</language>
    <item>
      <title>Building a SQL Lexer in Rust: Why I Replaced `Vec&lt;char&gt;` with `&amp;str` and `Ident(String)` with Spans</title>
      <dc:creator>Musab Khan</dc:creator>
      <pubDate>Sun, 31 May 2026 09:33:51 +0000</pubDate>
      <link>https://dev.to/musab0986/building-a-sql-lexer-in-rust-why-i-replaced-vec-with-str-and-identstring-with-spans-2ng2</link>
      <guid>https://dev.to/musab0986/building-a-sql-lexer-in-rust-why-i-replaced-vec-with-str-and-identstring-with-spans-2ng2</guid>
      <description>&lt;p&gt;I've been building a database engine from scratch in Rust, and I recently finished the lexer.&lt;/p&gt;

&lt;p&gt;The lexer itself wasn't the most interesting part.&lt;/p&gt;

&lt;p&gt;What I found more valuable was how my design evolved as I learned more about Rust and how compilers and database systems are typically implemented.&lt;/p&gt;

&lt;h2&gt;
  
  
  My First Approach
&lt;/h2&gt;

&lt;p&gt;When I started, I stored the input as a &lt;code&gt;Vec&amp;lt;char&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It felt straightforward because I could access characters directly without worrying about UTF-8 boundaries.&lt;/p&gt;

&lt;p&gt;I also represented identifiers like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nf"&gt;Ident&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, this seems perfectly reasonable.&lt;/p&gt;

&lt;p&gt;Every identifier token carries its own text, making it easy for the parser to consume.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;As the lexer grew, I started asking myself a simple question:&lt;/p&gt;

&lt;p&gt;The identifier already exists in the original SQL query.&lt;/p&gt;

&lt;p&gt;Why am I allocating another string and copying the same data into every token?&lt;/p&gt;

&lt;p&gt;For a query like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the source text already contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username
email
users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating separate &lt;code&gt;String&lt;/code&gt; allocations for each identifier means duplicating data that already exists.&lt;/p&gt;

&lt;p&gt;I also learned an important detail about Rust enums.&lt;/p&gt;

&lt;p&gt;The size of an enum is influenced by its largest variant.&lt;/p&gt;

&lt;p&gt;Once variants start carrying additional data, every token instance becomes larger than it otherwise needs to be.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving to a Span-Based Design
&lt;/h2&gt;

&lt;p&gt;Instead of storing identifier text directly inside tokens, I switched to storing only the token kind:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;Ident&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;along with source location information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;Span&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the token only answers two questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is this token?&lt;/li&gt;
&lt;li&gt;Where did it come from?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the parser needs the actual identifier text, it can recover it directly from the original SQL source using the stored byte range.&lt;/p&gt;

&lt;h2&gt;
  
  
  Replacing &lt;code&gt;Vec&amp;lt;char&amp;gt;&lt;/code&gt; with &lt;code&gt;&amp;amp;str&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The second design change was moving away from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and operating directly on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;using lifetimes.&lt;/p&gt;

&lt;p&gt;Instead of creating another collection containing the entire input, the lexer now walks over borrowed source text.&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No duplicated input buffer&lt;/li&gt;
&lt;li&gt;Less memory usage&lt;/li&gt;
&lt;li&gt;Fewer allocations&lt;/li&gt;
&lt;li&gt;A single source of truth&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The lexer doesn't own the SQL string.&lt;/p&gt;

&lt;p&gt;It only borrows it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Current Output
&lt;/h2&gt;

&lt;p&gt;For the query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the lexer produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select @ line 1, col 1, bytes 0..6
Ident @ line 1, col 8, bytes 7..11
Comma @ line 1, col 12, bytes 11..12
Ident @ line 1, col 14, bytes 13..16
From @ line 1, col 18, bytes 17..21
Ident @ line 1, col 23, bytes 22..27
Where @ line 1, col 29, bytes 28..33
Ident @ line 1, col 35, bytes 34..37
Gt @ line 1, col 39, bytes 38..39
IntLit(18) @ line 1, col 41, bytes 40..42
Semicolon @ line 1, col 43, bytes 42..43
Eof @ line 1, col 44, bytes 43..43
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;I started this project to understand how databases work internally.&lt;/p&gt;

&lt;p&gt;What surprised me most so far wasn't SQL.&lt;/p&gt;

&lt;p&gt;It was seeing how a few seemingly small design decisions around ownership, borrowing, and data representation can significantly change the memory characteristics of a system.&lt;/p&gt;

&lt;p&gt;The lexer is complete.&lt;/p&gt;

&lt;p&gt;Next stop: building the parser and AST.&lt;/p&gt;

&lt;p&gt;If you've built a compiler, interpreter, database, or parser before, I'd be interested to hear what design decisions ended up changing your implementation the most.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>database</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
