Just An Application

June 14, 2013

Programming With Rust — Part Seven: Things We Need To Know – Strings And String Literals

1.0 The String Type

A Rust string is an immutable sequence of UTF-8 encoded Unicode characters of type str.

2.0 String Literals

A Rust string literal is a sequence of Unicode characters delimited by double-quotes.

As in other programming languages, to include a double-quote (‘”‘) in a string literal you must escape it using a backslash (‘\’).

3.0 Memory Allocation

A Rust string can be allocated in the managed heap of a Task

    let cod: @str = @"cod";

or the owned heap

    let dab: ~str = ~"dab";

but not on the stack.

A string literal that is not explicitly allocated in a managed heap or in the owned heap is allocated in static memory.

You can obtain a borrowed pointer to a string literal allocated in static memory like this

    let eel: &str = "eel";

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.

Blog at WordPress.com.

%d bloggers like this: