Just An Application

June 10, 2013

Programming With Rust — Part Two: Things We Need To Know – Some Types

Filed under: Programming Languages, Rust, Rust 0.6, Rust On Mac OS X — Tags: , , , — Simon Lewis @ 9:15 pm

1.0 Some Primitive Types

1.1 The uint type

A value of type uint is an unsigned integer.

The size of a uint, that is the number of bits used to represent one, is machine dependent, and is specified to be

equal to the number of bits required to hold any memory address on the target machine.

No prizes for guessing what else these might be being used for in the implementation.

1.2 The unit Type

The unit type is a type with a single zero-size value.

Both the type and the value are specified like this

    ()

2.0 Enums

A Rust enum type defines a union type.

For example, in graphics programming there are any number of different representations of a colour.

You can use an enum type to define a Colour type in terms of a number of different representation types like this

    enum Colour
    {
        CYMK(uint, uint, uint, uint),
        HSB (uint, uint, uint),
        RGB (uint, uint, uint),
    }

Given this definition a value of type Colour can be a value of either

  • the CYMK type, or

  • the HSB type, or

  • the RGB type

A value of a variant type defined by an enum type can be constructed by using a function with the same name as the variant type.

For example,

    CYMK(0, 0, 0, 0)

or

    HSB(39, 0, 100)

or

    RGB(255, 255, 255)

These functions are defined automatically as part of the enum type definition.

When used in this way it is evident that a Rust enum type is not the same as a C++ or a Java enum type.

However when defining a Rust enum type it is possible to specify variant types which have no associated data, like this

    enum PrimaryColour
    {
        Red,
        Green,
        Blue
    }

This form of Rust enum type is equivalent to a C++ enum type.

3.0 Generic Types

Rust supports generic Types using the ‘<...>‘ syntax familiar from C++ and latterly Java.

For example, you could, if you really wanted to, define a generic version of the Colour enum type above like this

    enum Colour<T>
    {
        CYMK(T, T, T, T)
        HSB (T, T, T),
        RGB (T, T, T),
    }

The instantiated type

    Colour<uint>

would then be equivalent to the original non-generic definition of the Colour type.


Copyright (c) 2013 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.

Advertisements

4 Comments »

  1. […] simple discrete values such as integers as well as more complicated aggregates of values such as enum […]

    Pingback by Programming With Rust — Part Five: Things We Need To Know – Tasks And Memory | Just An Application — June 13, 2013 @ 9:59 am

  2. […] instance of the Ok variant with an associated value of the unit type, that is, nothing, […]

    Pingback by Programming With Rust — Part Eight: Invoking The Listen Function | Just An Application — June 14, 2013 @ 9:41 am

  3. […] is a variant enum. […]

    Pingback by Rust Revisted: Ch…ch…ch…changes Or No More Sockets Anymore | Just An Application — July 26, 2014 @ 4:50 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: