R
RUSTY YELLOW PAGES

LangColorMap

This is an index I went looking for and could not find. The goal is a different colour for every kind of syntax, so that code can be understood much more quickly without reading all of it — you see a certain colour and you already know what something is, before you have actually read the word. It looks bad right now, I know. I am still tuning it, and I want to open it up so other people can have a go at setting the colours better too.

Ordered by hue, so neighbouring entries are neighbouring colours. Hover a row for what it covers.


Preview on
call-free #D9A98A#D9A98A
string #FFAE00#C23B24
constant #E5D9A8#C29B00
lifetime #F2DB69#FD4EDD
call-method #FFEA00#7004C8
call-assoc #FFF952#F5A55A
punct #FBF42D#180070
module #00FA1D#AA261D
type-def #1FFF9E#1189DF
variable #6BFFEE#06C3C6
trait #02AEC5#019D59
type #00E1FF#2B6B73
field #A8E2FF#8280FF
generic-param #A9D6F5#C89797
fn-def #B899FF#9966A9
macro #D972EE#D972EE
attribute #FED6FF#752F00
number #A17297#0939C8
comment #FFFFFF#156C04
keyword #C2BBC3#FA0000

Every role, one snippet

Each colour above appears at least once below.

#![allow(dead_code)]

use std::collections::HashMap;

const MAX_ENTRIES: usize = 128;
static UNIT: char = '·';

/// Counts words, keyed by label.
#[derive(Debug, Clone)]
pub struct Counter<'a, T> {
    label: &'a str,
    counts: HashMap<String, T>,
    ratio: f64,
}

pub enum Mode {
    Fast,
    Careful,
}

pub trait Summarize {
    fn summary(&self) -> String;
}

impl<'a, T: Default> Counter<'a, T> {
    pub fn new(label: &'a str) -> Self {
        Counter {
            label,
            counts: HashMap::new(),
            ratio: 0.5,
        }
    }

    fn record(&mut self, word: &str) -> Option<usize> {
        if self.counts.len() >= MAX_ENTRIES {
            return None;
        }
        self.counts.insert(word.to_string(), T::default());
        Some(self.counts.len())
    }

    fn first_len(&self) -> Option<usize> {
        let len = self.counts.keys().next()?.len();
        Some(len)
    }
}

impl<'a, T: Default> Summarize for Counter<'a, T> {
    fn summary(&self) -> String {
        format!("{} {UNIT} {}", self.label, self.counts.len())
    }
}

fn tally(values: &[usize]) -> usize {
    values.iter().copied().sum()
}

fn main() {
    let mut counter = Counter::<String>::new("words");
    let added = counter.record("ferris").unwrap_or(0);
    let doubled: Vec<usize> = [1, 2, 3].iter().map(|n| n * 2).collect();
    let total = tally(&doubled);
    let verbose: bool = true;
    let mode = Mode::Fast;
    if let Mode::Careful = mode { return; }

    if verbose && added > 0 {
        println!("{} {}", counter.summary(), total);
    }

    unsafe {
        let raw = &UNIT as *const char;
        let _ = *raw;
    }

    'outer: for value in &doubled {
        if *value == 4 { break 'outer; }
    }
}

Use it in VS Code

The same table generates a VS Code theme, so an editor can paint code the way this page does. It needs rust-analyzer: the role colours ride on its semantic tokens, which is how the editor tells Type::new() from value.method(). Without it you get comments, literals and keywords and nothing more.

  1. Download the vscode-theme folder from the repository.
  2. Copy it into your extensions directory: %USERPROFILE%\.vscode\extensions\ on Windows, ~/.vscode/extensions/ on macOS and Linux.
  3. Reload the window — Ctrl+Shift+P, then Developer: Reload Window. VS Code only reads that folder at startup, so it will not appear before you do.
  4. Choose it with Ctrl+K Ctrl+T: Rusty Yellow Pages Dark or Light.

For an installable package instead, run npx @vscode/vsce package inside that folder and install the .vsix it produces through Extensions: Install from VSIX…. Give rust-analyzer a moment to finish indexing after you switch — the role colours only arrive once it has.