# Declare variables with valid range?

**URL:** <https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049>\
**Category:** ideas (deprecated)\
**Created:** [December 19, 2014, 3:46pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049 "2014-12-19T15:46:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![BryanQuigley](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@BryanQuigley](https://internals.rust-lang.org/u/BryanQuigley)\
**Post date:** [December 19, 2014, 3:46pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/1 "2014-12-19T15:46:56Z")

</div>

It seems like a natural extension of how variables (immutable by default, mutable if specified) are defined to allow the programmer to dictate a specific range of allowed values for an integer. If I know a value is only valid between 0-1000 the sooner I declare that the better it is for catching bugs, off by one errors, and more…

I’m not sure what exact syntax would work, maybe:

let mut(0,1000) x = 0i;

x is only valid from 0-1000 inclusive.

Thoughts?

---

<div class="post-metadata">

**Author:** ![tupshin](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tupshin/32/2573_2.png) [@tupshin](https://internals.rust-lang.org/u/tupshin)\
**Post date:** [December 19, 2014, 3:55pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/2 "2014-12-19T15:55:45Z")

</div>

This would require bounds checking ([http://en.wikipedia.org/wiki/Bounds\_checking](http://en.wikipedia.org/wiki/Bounds_checking)) on every calculation, but that is a not unreasonable tradeoff in many cases.

---

<div class="post-metadata">

**Author:** ![Uther](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/uther/32/6225_2.png) [@Uther](https://internals.rust-lang.org/u/Uther)\
**Post date:** [December 19, 2014, 4:16pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/3 "2014-12-19T16:16:40Z")

</div>

This RFC suggested range checking, but was not accepted (too complex) :

> <https://github.com/rust-lang/rfcs/blob/61969f5cbbfc5d79510419b62a766b05e76d88b3/active/0000-no-integer-overflow.md>

---

<div class="post-metadata">

**Author:** ![mrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mrec/32/4002_2.png) [@mrec](https://internals.rust-lang.org/u/mrec)\
**Post date:** [December 19, 2014, 6:21pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/4 "2014-12-19T18:21:34Z")

</div>

Doesn’t Rust have enough support for operator overloading to be able to build a reasonable RangedNumber library without needing explicit language support? That seems to have been enough for C++ users over the years. I suppose it might be nice to have range checking for the builtin types available in debug builds, but I’d be very wary of putting a range constraint in code with no guarantee that it’d be enforced in any way.

Twenty years ago when I did some work in Ada for 68020s, it was general practice to put range constraints on everything that moved. I don’t remember anybody ever questioning it, even in a fairly performance-sensitive environment, but I was very wet behind the ears at the time and don’t know how many real bugs this caught in practice.

---

<div class="post-metadata">

**Author:** ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)\
**Post date:** [December 19, 2014, 8:14pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/5 "2014-12-19T20:14:09Z")

</div>

Not if you want to encode the range in the type, since generic arguments can’t be values. (Well, not without some nasty hack way to encode a number into a type. When constants as associated items are implemented, it won’t be as nasty, though…)

---

<div class="post-metadata">

**Author:** ![seanmonstar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/seanmonstar/32/36_2.png) [@seanmonstar](https://internals.rust-lang.org/u/seanmonstar)\
**Post date:** [December 19, 2014, 9:57pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/6 "2014-12-19T21:57:12Z")

</div>

For smallish types, you can use an enum to restrict possible values. But it gets unwieldy as the range gets bigger.

---

<div class="post-metadata">

**Author:** ![fan-tom](https://avatars.discourse-cdn.com/v4/letter/f/82dd89/32.png) [@fan-tom](https://internals.rust-lang.org/u/fan-tom)\
**Post date:** [January 14, 2017, 7:59am UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/7 "2017-01-14T07:59:12Z")

</div>

If bounds checking would be performed in runtime, it is not Rust way (zero-cost abstraction). But elsewhere it requires dependent types support and compiler must prove type safety in that case by proving each usage does not try to access value out of bounds. Type checking in such type system may be undecidable, because you cannot even prove equality of two types if they depends of runtime values (function that return list of words it read from stdin, what return type it may have?)

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [January 16, 2017, 4:51pm UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/8 "2017-01-16T16:51:03Z")

</div>

I’m waiting for Rust to get values in generic types, so that it’s possible to implement it as a library.

OTOH if it was built into the language, it could be used in `Option` optimization (e.g. so that `Option<u31>` could fit in a 32 bit register).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:23am UTC](https://internals.rust-lang.org/t/declare-variables-with-valid-range/1049/9 "2019-03-25T08:23:23Z")

</div>

Closed on March 25, 2019, 8:23am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
