Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rust, can you own a string literal?

According to The Rust book:

Each value in Rust has a variable that’s called its owner. There can be only one owner at a time. When the owner goes out of scope, the value will be dropped.

According to rust-lang.org:

Static items do not call drop at the end of the program.

After reading this SO post, and given the code below, I understand that foo is a value whose variable y, equivalent to &y since "string literals are string slices", is called its owner. Is that correct? Or do static items have no owner?

let x = String::from("foo");  // heap allocated, mutable, owned
let y = "foo" // statically allocated to rust executable, immutable

I'm wondering because unlike an owned String, string literals are not moved, presumably because they're stored in .rodata in the executable.

fn main() {
  let s1 = "foo"; // as opposed to String::from("foo")
  let s2 = s1; // not moved
  let s3 = s2; // no error, unlike String::from("foo")
}

UPDATE: According to The Rust book:

These ampersands are references, and they allow you to refer to some value without taking ownership of it...Another data type that does not have ownership is the slice.

Since string literals are string slices (&str) (see citation above), they, logically, do not have ownership. The rationale seems to be that the compiler requires a data structure with a known size: a reference:

let s1: str = "foo"; // [rustc E0277] the size for values of type `str` cannot be known at compilation time [E]
like image 691
mellow-yellow Avatar asked Jul 26 '19 18:07

mellow-yellow


People also ask

Can a string be a literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.

What is a string literal Rust?

According to the The Rust Reference 1, A string literal is a sequence of any Unicode characters enclosed within two U+0022 (double-quote) characters, with the exception of U+0022 itself 2. Escape characters in the string literal body are processed. The string body cannot contain a double-quote.

Is it possible to modify a string literally?

The behavior is undefined if a program attempts to modify any portion of a string literal. Modifying a string literal frequently results in an access violation because string literals are typically stored in read-only memory.

Are strings in Rust immutable?

Rust owned String type, the string itself lives on the heap and therefore is mutable and can alter its size and contents.


1 Answers

A string slice reference (&str) does not own the string slice that it points to, it borrows it. You can have several immutable references to an object, that's why your second code sample is correct and the borrow checker is happy to accept it.

I think you can say that types with the 'static lifetime have no owner, or that something outside of the main function owns it. The owner only matters when the lifetime of the owning object ends (if you own it, you need to free resources). For references only lifetimes matter.

like image 196
magras Avatar answered Oct 03 '22 15:10

magras