pub struct Symbol(/* private fields */);Expand description
A small, copyable handle to a string held by an Interner.
A Symbol is a newtype over a 32-bit id, so it is four bytes, Copy, and as
cheap to pass or store as an integer. The point of interning is that a name in
an AST node or a symbol-table key costs a Symbol instead of an owned
String, and comparing two names becomes an integer comparison instead of a
byte-by-byte walk — ==, Ord, and Hash on a Symbol never touch the
underlying bytes.
A Symbol is only meaningful together with the interner that issued it. Two
symbols from the same interner are equal exactly when they name the same
string; symbols from different interners share no relationship and should not
be compared or resolved across interners (resolving a foreign symbol is
handled safely — see Interner::resolve — but the
result is not meaningful).
§Examples
use intern_lang::Interner;
let mut interner = Interner::new();
let a = interner.intern("loop");
let b = interner.intern("loop");
// Same string interns to the same symbol, so equality is an integer compare.
assert_eq!(a, b);
// A symbol is `Copy`: handing it around never moves or clones a string.
let copy = a;
assert_eq!(copy, a);Implementations§
Source§impl Symbol
impl Symbol
Sourcepub fn as_u32(self) -> u32
pub fn as_u32(self) -> u32
Returns the raw 1-based integer id backing this symbol.
The id is stable for the lifetime of the issuing interner and is assigned
in interning order: the first distinct string interned gets 1, the
second 2, and so on. It is exposed for diagnostics, stable ordering, and
building external side tables keyed by symbol; it is not a string and
carries no meaning outside the interner that produced it.
§Examples
use intern_lang::Interner;
let mut interner = Interner::new();
let first = interner.intern("alpha");
let second = interner.intern("beta");
assert_eq!(first.as_u32(), 1);
assert_eq!(second.as_u32(), 2);
// Re-interning an existing string returns the same id, never a new one.
assert_eq!(interner.intern("alpha").as_u32(), 1);