您是否考虑过它可能使用str
而不是只有长度而没有容量的String并且
您正在查看虚假的第三个值的可能性?
:\>cat main.rs
fn main() {
let s1 = String::from("Hello, std::world!");
println!("{}",s1);
println!("{} {}",s1.capacity() , s1.len());
let s2 = "Hello, std::world!o";
println!("{}",s2);
println!("{} {}",s2.capacity() , s2.len());
}
:\>cargo build
Compiling hello_world v0.1.0
error[E0599]: no method named `capacity` found for reference `&str` in the current scope
--> main.rs:7:22
|
7 | println!("{} {}",s2.capacity() , s2.len());
| ^^^^^^^^ method not found in `&str`
如下所示的第一个字符串
0:000> dv /v
00000064`f0cff920 s2 = struct str*
00000064`f0cff8c0 s1 = "Hello, std::world!"
0:000> dpa 00000064`f0cff920 l3
00000064`f0cff920 00007ff6`65cf2638 "Hello, std::world!o"
00000064`f0cff928 00000000`00000013
00000064`f0cff930 00000000`00000010 <<<<<<< bogus garbage
0:000> dpa 00000064`f0cff8c0 l3
00000064`f0cff8c0 00000174`116191f0 "Hello, std::world!.............................."
00000064`f0cff8c8 00000000`00000012
00000064`f0cff8d0 00000000`00000012 <<<<<<<<<<<<< correct