Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Rust struct with string members?

Tags:

string

rust

I want the members to be owned by the struct. Sorry for the simple question, but I wasn't able to find an example. I'm looking for the correct declaration of a struct and instantiation examples.

like image 572
vladimir Avatar asked Sep 09 '14 23:09

vladimir


People also ask

How do you write a struct in Rust?

To define a struct, we enter the keyword struct and name the entire struct. A struct's name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call fields.

How are strings stored in rust?

A String is stored as a vector of bytes ( Vec<u8> ), but guaranteed to always be a valid UTF-8 sequence. String is heap allocated, growable and not null terminated. &str is a slice ( &[u8] ) that always points to a valid UTF-8 sequence, and can be used to view into a String , just like &[T] is a view into Vec<T> .

What is the difference between STR and string in Rust?

String is the dynamic heap string type, like Vec : use it when you need to own or modify your string data. str is an immutable1 sequence of UTF-8 bytes of dynamic length somewhere in memory. Since the size is unknown, one can only handle it behind a pointer.

How do you access struct members in Rust?

Once an instance of a struct exists, we can access its members with dot notation. To enable members to be changed, we must mark the individual struct instance as mutable with the mut keyword. We can pass a struct to a function by specifying the struct name as the type in the parameter list.


1 Answers

If the string has to be owned by the struct, then you should use String. Alternatively, you could use an &str with a static lifetime (i.e., the lifetime of the program). For example:

struct Foo {     bar: String,     baz: &'static str, }  fn main() {     let foo = Foo {         bar: "bar".to_string(),         baz: "baz",     };     println!("{}, {}", foo.bar, foo.baz); } 

If the lifetime of the string is unknown, then you can parameterize Foo with a lifetime:

struct Foo<'a> {     baz: &'a str, } 

See also:

  • What are the differences between Rust's `String` and `str`?

If you're not sure whether the string will be owned or not (useful for avoiding allocations), then you can use borrow::Cow:

use std::borrow::Cow;  struct Foo<'a> {     baz: Cow<'a, str>, }  fn main() {     let foo1 = Foo {         baz: Cow::Borrowed("baz"),     };     let foo2 = Foo {         baz: Cow::Owned("baz".to_string()),     };     println!("{}, {}", foo1.baz, foo2.baz); } 

Note that the Cow type is parameterized over a lifetime. The lifetime refers to the lifetime of the borrowed string (i.e., when it is a Borrowed). If you have a Cow, then you can use borrow and get a &'a str, with which you can do normal string operations without worrying about whether to allocate a new string or not. Typically, explicit calling of borrow isn't required because of deref coercions. Namely, Cow values will dereference to their borrowed form automatically, so &*val where val has type Cow<'a, str> will produce a &str.

like image 184
BurntSushi5 Avatar answered Sep 27 '22 21:09

BurntSushi5