Just An Application

June 17, 2013

Programming With Rust — Part Eleven: Even More Things We Need To Know – The Portmanteau Edition

1.0 Machine Independent Integers

Rust supports explicitly sized, and hence machine independent, signed and unsigned integer types.

The supported signed integer types are

  • i8

  • i16

  • i32

  • i64

The supported unsigned integer types are, unsurprisingly

  • u8

  • u16

  • u32

  • u64

2.0 Vectors

A Rust vector type specifies a one-dimensional array of values of the same type.

The vector type can specify an exact size

   [u8, ..512]

in which case it can be allocated on the stack.

If the type does not specify an exact size then it cannot be allocated on the stack and the type declaration must identify where it should be allocated, which is either in the managed heap of the current task, like so

   @[u8]

or in the owned heap, like so

   ~[u8]

A value of a vector type can be specified by explicitly enumerating all of its elements

    let o_vec: ~[u8] = ~[0, 1, 2, 3];

or by specifying an initial value and a size.

   let s_vec: [u8, ..512] = [0u8, ..512];

Individual elements of a vector can be accessed by indexing as you might expect.

For example

   let element_zero: u8 = s_vec[0];

3.0 Structs

A Rust struct type defines a set of named values.

For example

    struct QName
    {
        prefix:    ~str,
        nsURI:     ~str,
        localPart: ~str
    }

This is an example of a struct item.

3.1 Creating Values

A value of a struct type is created using a struct expression which has a similar syntax to a struct item

    typename '{' [fieldname ':' expression] [',' fieldname ':' expression ]* '}'

For example you can create an value of type QName like this

    let rights = QName {prefix:~"dc", nsURI:~"http://purl.org/dc/elements/1.1/", localPart:~"rights"};

This creates an immutable value of type QName in the current stack frame.

You can also create a value of a struct type in the managed heap, for example

    let m_rights = @QName {prefix:~"dc", nsURI:~"http://purl.org/dc/elements/1.1/", localPart:~"rights"};

or in the owned heap

    let o_rights = ~QName {prefix:~"dc", nsURI:~"http://purl.org/dc/elements/1.1/", localPart:~"rights"};

3.2 Direct Field Access

You can access the fields of a value of struct type using dot (.) notation, like so

    ...

    let rights_prefix:    &str = rights.prefix;
    let rights_nsURI:     &str = rights.nsURI;
    let rights_localPart: &str = rights.localPart;

    ...

3.3 Access Control

The fields of a value of a struct type are public by default but they can be made private like so

    struct QName
    {
        priv prefix:    ~str,
        priv nsURI:     ~str,
        priv localPart: ~str
    }

The effect of this is to limit construction of values of the struct type, and direct field access, to code defined in the module in which the struct is defined.

4.0 Tuples

A Tuple is an immutable fixed-size collection of values.

A tuple value is written as a parentheses delimited list of comma separated values.

A tuple type is written as a parentheses delimited list of comma separated types.

For example, extending the field access example above, we can do this

    ...

    let rights_prefix:    &str = rights.prefix;
    let rights_nsURI:     &str = rights.nsURI;
    let rights_localPart: &str = rights.localPart;
    
    ...
    
    let rights_tuple:  (&str, &str, &str) = (rights_prefix, rights_nsURI, rights_localPart);

    ...

The only way to access the elements of a tuple is by pattern matching.

For example

   ...

    let (prefix, nsURI, localPart) = rights_tuple;
    
    io::println(fmt!("prefix     == %?", prefix));
    io::println(fmt!("nsURI      == %?", nsURI));
    io::println(fmt!("localPart  == %?", localPart));

    ...

prints

    prefix     == "dc"
    nsURI      == "http://purl.org/dc/elements/1.1/"
    localPart  == "rights"

You can achieve the same effect by using a match expression like this

   match rights_tuple
    {
        (prefix, nsURI, localPart) =>
        {
            io::println(fmt!("prefix     == %?", prefix));
            io::println(fmt!("nsURI      == %?", nsURI));
            io::println(fmt!("localPart  == %?", localPart));
        }
    }

5.0 Argument Passing, Value Returning, And Other Things Of That Ilk

Arguments are passed by value using a shallow copy, so if the argument is a composite value containing pointers then it is only a copy
of the value itself, any pointers it contains point to the same things as the original.

The same is true for return values and for assignment.

As we have already seen, if the value being passed, returned or assigned is an owned pointer then the copy becomes a move as the original is invalidated.

The use of a shallow copy for composite values means that the same is necessarily true if those values directly or indirectly contain one or more owned pointers.

For example, if we have a function defined like this

    fn start_element(name: QName)
    {
        ...
    }

and we invoke it with a stack allocated QName value as its argument like this

    let rights = QName { prefix:~"dc", nsURI:~"http://purl.org/dc/elements/1.1/", localPart:~"rights"};

    start_element(rights);
    
    ...

then after the call to start_element the local variable rights is invalid.

This is because a QName value contains three owned pointers, so when it is passed to the function using a shallow copy the ownership of those owned pointers must be transferred, so the original value must be invalidated as it is no longer usable.


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.

Create a free website or blog at WordPress.com.