slice和数组之间有什么区别? [英] What is the difference between a slice and an array?

查看:226
本文介绍了slice和数组之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,为什么&[u8]&[u8; 3]都正常吗?

Why are both &[u8] and &[u8; 3] ok in this example?

fn main() {
    let x: &[u8] = &[1u8, 2, 3];
    println!("{:?}", x);

    let y: &[u8; 3] = &[1u8, 2, 3];
    println!("{:?}", y);
}

&[T; n]可以强制为&[T]的事实是使它们可以容忍的一个方面. 克里斯·摩根(Chris Morgan)

The fact that &[T; n] can coerce to &[T] is the aspect that makes them tolerable. — Chris Morgan

为什么&[T; n]强制转换为&[T]?这种强迫还会在其他什么条件下发生?

Why can &[T; n] coerce to &[T]? In what other conditions does this coercion happen?

推荐答案

[T; n]是长度为n的数组,表示为与T相邻的T实例.

[T; n] is an array of length n, represented as n adjacent T instances.

&[T; n]纯粹是对该数组的引用,表示为指向数据的瘦指针.

&[T; n] is purely a reference to that array, represented as a thin pointer to the data.

[T]是切片,未调整大小的类型;它只能通过某种形式的间接使用.

[T] is a slice, an unsized type; it can only be used through some form of indirection.

&[T],称为切片,是一种大小类型.这是 fat指针,表示为指向第一项和切片长度的指针.

&[T], called a slice, is a sized type. It's a fat pointer, represented as a pointer to the first item and the length of the slice.

因此,在编译时,数组的长度是已知的,而切片的长度是运行时的问题.数组目前在Rust中是二等公民,因为不可能形成数组泛型.对[T; 0][T; 1]& c.的各种特征都有手动实现,通常最多32个.由于此限制,切片通常更为有用. &[T; n]可以强制为&[T]的事实是使它们可以忍受的一个方面.

Arrays thus have their length known at compile time while slice lengths are a runtime matter. Arrays are second class citizens at present in Rust, as it is not possible to form array generics. There are manual implementations of the various traits for [T; 0], [T; 1], &c., typically up to 32; because of this limitation, slices are much more generally useful. The fact that &[T; n] can coerce to &[T] is the aspect that makes them tolerable.

对于[T; 3],有一个fmt::Debug实现,其中T实现了Debug,而对于&T还有一个实现,其中T实现了fmt::Debug,而u8实现了Debug&[u8; 3]也是.

There is an implementation of fmt::Debug for [T; 3] where T implements Debug, and another for &T where T implements fmt::Debug, and so as u8 implements Debug, &[u8; 3] also does.

为什么&[T; n]强制转换为&[T]?在Rust中,什么时候发生胁迫?

Why can &[T; n] coerce to &[T]? In Rust, when does coercion happen?

它会在需要时强制运行,而不会在其他时间强制运行.我可以想到两种情况:

It will coerce when it needs to and at no other times. I can think of two cases:

  1. 某处期望&[T]的地方,而您给它一个&[T; n]的地方,它将默默地胁迫;
  2. 当您在[T; n]上调用x.starts_with(…)时,将观察到[T; n]上没有这样的方法,因此autoref起作用并尝试&[T; n],这无济于事,然后强制起作用并尝试&[T],该方法具有称为starts_with的方法.
  1. where something expects a &[T] and you give it a &[T; n] it will coerce silently;
  2. when you call x.starts_with(…) on a [T; n] it will observe that there is no such method on [T; n], and so autoref comes into play and it tries &[T; n], which doesn’t help, and then coercion come into play and it tries &[T], which has a method called starts_with.

代码段[1, 2, 3].starts_with(&[1, 2])演示了这两者.

这篇关于slice和数组之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆