Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a `&str` contain a pointer to program memory in Rust?

Section 4.3 about slices of The Rust Programming Language has this paragraph:

String Literals Are Slices

Recall that we talked about string literals being stored inside the binary. Now that we know about slices, we can properly understand string literals:

let s = "Hello, world!";

The type of s here is &str: it’s a slice pointing to that specific point of the binary. This is also why string literals are immutable; &str is an immutable reference.

Now, a string slice is, as they've covered earlier, a pair (of some sort) containing a pointer and a length. To me, this paragraph (specifically "we talked about string literals being stored inside the binary" and "pointing to that specific point of the binary") seems to imply that that pointer goes straight into program memory. Not the stack, not the heap, but the actual place where the processor stores all the instructions that the program consists of. A specific line of the assembly, if you will.

Is this true? If not, what else could they mean by that, and how is it actually done? How can I figure this out myself if I have a similar question later along the line?

like image 586
Arthur Avatar asked Aug 13 '18 14:08

Arthur


People also ask

Can a girl get pregnant on her period?

Yes — it's possible to get pregnant if you have unprotected sex during your period. However, it's less common for this to happen. Here's the deal: A woman is most likely to get pregnant from sex that happens just before and during ovulation (when an egg is released).

Can a woman get pregnant without a man?

Pregnancy without sperm — is it possible? Although you can get pregnant without having sexual intercourse, pregnancy without sperm is impossible. Without intercourse, you can get pregnant with the help of different fertility treatments and procedures such as IVF, IUI, and at-home insemination.

What does can you can a can mean?

"Can you can a can" means can you put food in a can; "as a canner" is in reference to a person putting food in a can in a factory; "can a can" or "as a canner can can a can" means a person who puts food in a can and closing the can. A canner was a person in a factory who would can food (put food in can).

What is the difference between can and extended can?

The last part of the CAN message is the interframe space (IFS), which is being used as a time delay. Extended CAN uses a 29 bit identifier with a couple additional bits. The extended 29 bit identifier (CAN 2.0B) is identical, but has a longer ID and is usually used in the j1939 protocol - heave-duty vehicles.

What is the difference between can could and may?

When we talk about possibility, we use can, could and may, but they are different in meaning. … We use can, could and may to ask for permission. We use can and may, but not could, to give permission. May is less common: …

Why buy a Can-Am?

Can-Am ATVs and side-by-side vehicles are designed to outperform, no matter what. From the trails to fields, dunes to mountaintops, this beautiful, challenging, exciting place is for those who dare to venture off-road for work or play. DISCOVER 2021 LINEUP DISCOVER CAN-AM WORLD.


1 Answers

It's true - &'static str string literals are embedded within the binary itself. This is not unique to Rust, e.g. C implementations often do the same thing.

Citing the Rust Reference (emphasis mine):

A string literal is a string stored directly in the final binary, and so will be valid for the 'static duration.

Its type is 'static duration borrowed string slice, &'static str.

There are programs designed specifically to find these kinds of strings, e.g. strings or just grep --text.

Related question:

  • String literals: Where do they go?
like image 121
ljedrz Avatar answered Oct 13 '22 12:10

ljedrz