---
title: "Niches for integer types in Rust"
url: https://daily.dev/posts/niches-for-integer-types-in-rust-grufxq4r3
source_url: https://deterministic.space/niche-int-types-in-rust.html
type: article
source: "Pascal’s scribbles"
published: 2026-05-04T16:40:50.182Z
updated: 2026-05-04T16:41:14.535Z
tags: ["rust"]
reading_time: 5
upvotes: 3
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Niches for integer types in Rust

**[Pascal’s scribbles](https://daily.dev/sources/deterministicspace)** · 5 min read · 3 upvotes · 0 comments

## Summary

Explores how to create memory-efficient integer wrapper types in Rust using type niches. Starting with a DNA sequence position type (Pos) that only uses the u31 range, the post covers three approaches: a naive u32 wrapper (wastes 4 bytes in Option), the NonZero bias trick on stable Rust (stores value+1 to expose a niche, making Option<Pos> 4 bytes), and the experimental nightly pattern_type! macro that lets you declare a valid range directly (e.g., i32 is 0..=i32::MAX). The nightly approach is cleaner conceptually but requires unsafe transmute for now. The post also notes that the older rustc_layout_scalar_valid_range_start attributes are being replaced by pattern types.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://deterministic.space/niche-int-types-in-rust.html>

---

Tags: [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/niches-for-integer-types-in-rust-grufxq4r3)
