简体   繁体   中英

How does Rust determine the length of a slice when unsizing from an array?

I know that a slice is like a fatptr: (data_ptr, len) . When I slice an array into a slice:

let arr = [0; 10];
let slice_arr = &arr[..];

An array doesn't have a length field like Vec<T> .

I know that slice_arr.len() can get the length of slice because slice_arr has a length field. How does Rust know the length field when an array is converted into a slice?

An array does have a length "parameter" of sorts. It's not a field, it's part of the type:

let x: [u8; 2] = [1, 2];
let y: [u8; 3] = x; // Error here

The type of x is [u8; 2] [u8; 2] . The number of elements is always 2, and the type of the elements is always u8 .

Because [u8; 2] [u8; 2] and [u8; 3] [u8; 3] are distinct types, one is always exactly 2 u8 s long, and the other is always exactly 3 u8 s long, the assignment from x into y fails.

When performing certain operations with arrays, the compiler has special built-in semantics for the arrays. These built-in semantics can make use of the length "parameter" (The N in [T; N] ). You can manipulate and access this value at the type level using const generics, but that's a nightly-only feature right now.

Use the .len method.

use std::io::stdin;

fn main() {
    let arr = [0; 10];
    let slice_arr = &arr[..];
    println!("first element of the slice: {}", slice_arr[0]);
    println!("the slice has {} elements", slice_arr.len());
}

https://doc.rust-lang.org/stable/rust-by-example/primitives/array.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM