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.

Advertisements

Leave a Comment »

No comments yet.

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

Create a free website or blog at WordPress.com.

%d bloggers like this: