A collection of intervals

Now that I have a simple interval I need a collection of them to represent the available ids that we can allocate. I’m going to create a new file for this new struct, intervals.rs and move the code from last time into interval.rs. I realise that I could leave it all in lib.rs but that doesn’t feel right to me and for now I’ll live with the extra complexity produced by the modules created by the different files that I’m using.

An interval

We’ll start with an interval, this is a struct with an upper and lower limit and represents an inclusive range, such as [0,255] or [1] where both upper and lower are the same value, 1. I’m sure there’s a better way to do this, such as using std::ops::Range<> but for now this is nice and simple. pub struct Interval { lower: u8, upper: u8, } For now this code lives in the lib.

An Id Manager

The first piece of code that I’m going to play with in Rust is a “reusable id manager”. This is a collection of numerical ids which can be allocated, used and then released back to a pool for later reuse. I tend to use this kind of code for network protocols where something like a peer-id is required; these ids can be within a given range (usually limited by the data type that represents them), and are “in use” for a period of time and then return to the collection.

Introduction

I’m a long term C++ programmer who is coming to Rust with lots of baggage. I’d like to write Rust in an idiomatic style but I may end up writing bad Rust in a C++ style. Hopefully people will help me avoid that. I’ve read the Rust Book1, or at least some of it. I tend to like to run before I can walk and get bored with the simple examples.