Skip to content

compile-time lookup tables via syntax extensions #4864

Closed

Description

@thestinger

It would be awesome to support building constant lookup tables at compile-time similar to gperf. This is a really common pattern and could potentially be a big performance win in many places (I'm sure rustc has a lot of constant tables).

A really simple proof of concept would sort an array and provide an object implementing the core::container::Map interface (once MutableMap is split out) via binary search.

The naming would be something along the lines of map!, set!, ordered_map! and ordered_set! along with the corresponding types ConstantMap, ConstantSet, ConstantOrderedMap, ConstantOrderedSet.

Activity

  1. graydon commented on Apr 30, 2013

    @graydon
    Contributor

    Agreed, but I think this would make a better 3rd party library (once syntax extensions are independently loadable) rather than necessarily part of the main compiler. I'll leave it open here since we don't have a syntax-extensions-wanted page yet.

  2. mstewartgallus commented on Jun 20, 2013

    @mstewartgallus
    Contributor

    A functional manner of doing this would be to use a match expression such as:

    fn lookup_table(key: &str) -> Option<uint> {
        match key {
            "foo" => Some(0),
            "bar" => Some(1),
            "car" => Some(2),
            _ => None
        }
    }
    

    A macro could easily be created that would provide a nicer syntax for this. Is there any reason that a hash table could not be used to implement big enough to benefit match expressions?

  3. sfackler commented on Jan 18, 2014

    @sfackler
    Member
  4. sfackler commented on Apr 14, 2014

    @sfackler
    Member

    I'm going to close this out as there's a decently mature implementation at https://github.com/sfackler/rust-phf

  5. added a commit that references this issue on Mar 4, 2026
  6. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions