(转)Rust:Vec、String 内存布局

xiaoxiao2021-02-28  85

https://stackoverflow.com/questions/21066133/what-is-the-difference-between-veci32-and-vecboxi32/21067103#21067103

一、Vec

let var1 :Vec<i32> =vec![1,2,3,4]; let var2:Vec<Box<i32>> =vec![Box::new(2),Box::new(2),Box::new(2),Box::new(2)];

二、borrow

let s2 =s1;

s1->s2 : ->no this style!

real style!

建议有兴趣可以阅读以下资料:

https://everstore.cn/a/oW-4NQbf9D

三、

此部分来源于:https://everstore.cn/a/f9xbPXAR-U

let v: Vec<f64> = vec![0.0, 0.707, 1.0, 0.707]; let a: [f64; 4] = [0.0, -0.707, -1.0, -0.707]; let sv: &[f64] = &v; let sa: &[f64] = &a;

在最后两行,Rust自动转换一个&Vec引用和&[f64; 4]引用到直接指向数据的切片引用。

最后,内存如下所示:

内存中的字符串

Rust 字符串是Unicode字符的序列,但它们不作为char的数组存储在内存中。 相反,它们使用UTF-8,一个可变宽的编码进行存储。 字符串中的每个ASCII字符都存储在一个字节中。 其他字符占用多个字节。

let noodles = "noodles".to_string(); let oodles = &noodles[1..]; let poodles = "ಠ_ಠ";

转载请注明原文地址: https://www.6miu.com/read-83142.html

最新回复(0)