`&str` 可以包含指向 Rust 中程序内存的指针吗?
Can a `&str` contain a pointer to program memory in Rust?
第 4.3 节关于 Rust 编程语言 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.
现在,正如他们之前介绍的那样,字符串切片是一对(某种)包含一个指针和一个长度。对我来说,这一段(特别是 "we talked about string literals being stored inside the binary" 和 "pointing to that specific point of the binary")似乎暗示该指针直接进入程序内存。不是堆栈,也不是堆,而是处理器存储程序所包含的所有指令的实际位置。如果你愿意的话,装配的特定行。
这是真的吗?如果不是,他们还有什么意思,实际上是如何完成的?如果我以后有类似的问题,我该如何自己解决这个问题?
这是真的 - &'static str
字符串文字嵌入在二进制文件本身中。这不是 Rust 独有的,例如.
引用 Rust Reference(强调我的):
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
.
有专门设计用于查找此类字符串的程序,例如strings or just grep --text
.
相关问题:
- String literals: Where do they go?
&str
不是单一结构,而是两个字节序列,对存储在只读数据部分中的实际文本进行编码(对于 LEF
,它是 .rodata
,但可以取决于可执行格式)的二进制文件和包含此序列开始位置及其长度(以字节为单位)信息的切片。
切片是一个双字对象,第一个字是指向数据的指针,第二个字是它的长度。
Rust 不是检查空字节以确定字符串的结束位置,而是跟踪该文本占用的字节数以了解它的结束位置。
第 4.3 节关于 Rust 编程语言 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.
现在,正如他们之前介绍的那样,字符串切片是一对(某种)包含一个指针和一个长度。对我来说,这一段(特别是 "we talked about string literals being stored inside the binary" 和 "pointing to that specific point of the binary")似乎暗示该指针直接进入程序内存。不是堆栈,也不是堆,而是处理器存储程序所包含的所有指令的实际位置。如果你愿意的话,装配的特定行。
这是真的吗?如果不是,他们还有什么意思,实际上是如何完成的?如果我以后有类似的问题,我该如何自己解决这个问题?
这是真的 - &'static str
字符串文字嵌入在二进制文件本身中。这不是 Rust 独有的,例如
引用 Rust Reference(强调我的):
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
.
有专门设计用于查找此类字符串的程序,例如strings or just grep --text
.
相关问题:
- String literals: Where do they go?
&str
不是单一结构,而是两个字节序列,对存储在只读数据部分中的实际文本进行编码(对于 LEF
,它是 .rodata
,但可以取决于可执行格式)的二进制文件和包含此序列开始位置及其长度(以字节为单位)信息的切片。
切片是一个双字对象,第一个字是指向数据的指针,第二个字是它的长度。
Rust 不是检查空字节以确定字符串的结束位置,而是跟踪该文本占用的字节数以了解它的结束位置。