问题描述
我正在阅读Rust的 Deref
特质:
pub trait Deref {
type Target: ?Sized;
fn deref(&self) -> &Self::Target;
}
deref
函数的类型签名对我来说似乎违反直觉.为什么返回类型是引用?如果引用实现了此特征,因此可以将其取消引用,那么这将产生什么影响?
我能提出的唯一解释是,引用未实现Deref
,但被认为是本来可取消引用的".但是,如何编写适用于任何可引用类型(包括Deref<T>
和&T
)的多态函数呢?
您可以看到实现Deref
的所有类型,并且&T
在该列表中:
impl<'a, T> Deref for &'a T where T: ?Sized
不明显的是,当您将*
运算符与实现Deref
的东西一起使用时,会应用语法糖.看看这个小例子:
use std::ops::Deref;
fn main() {
let s: String = "hello".into();
let _: () = Deref::deref(&s);
let _: () = *s;
}
error[E0308]: mismatched types
--> src/main.rs:5:17
|
5 | let _: () = Deref::deref(&s);
| ^^^^^^^^^^^^^^^^ expected (), found &str
|
= note: expected type `()`
found type `&str`
error[E0308]: mismatched types
--> src/main.rs:6:17
|
6 | let _: () = *s;
| ^^ expected (), found str
|
= note: expected type `()`
found type `str`
对deref
的显式调用返回&str
,但是运算符*
返回str
.就像您正在调用*Deref::deref(&s)
一样,忽略了隐式的无限递归.
尽管没用"有点强;对于实现Copy
的类型仍然有用.
另请参阅:
请注意,以上所有内容对于Index
和IndexMut
都是有效的.
I was reading the docs for Rust's Deref
trait:
pub trait Deref {
type Target: ?Sized;
fn deref(&self) -> &Self::Target;
}
The type signature for the deref
function seems counter-intuitive to me; why is the return type a reference? If references implement this trait so they can be dereferenced, what effect would this have at all?
The only explanation that I can come up with is that references don't implement Deref
, but are considered "primitively dereferenceable". However, how would a polymorphic function which would work for any dereferenceable type, including both Deref<T>
and &T
, be written then?
You can see all the types that implement Deref
, and &T
is in that list:
impl<'a, T> Deref for &'a T where T: ?Sized
The non-obvious thing is that there is syntactical sugar being applied when you use the *
operator with something that implements Deref
. Check out this small example:
use std::ops::Deref;
fn main() {
let s: String = "hello".into();
let _: () = Deref::deref(&s);
let _: () = *s;
}
error[E0308]: mismatched types
--> src/main.rs:5:17
|
5 | let _: () = Deref::deref(&s);
| ^^^^^^^^^^^^^^^^ expected (), found &str
|
= note: expected type `()`
found type `&str`
error[E0308]: mismatched types
--> src/main.rs:6:17
|
6 | let _: () = *s;
| ^^ expected (), found str
|
= note: expected type `()`
found type `str`
The explicit call to deref
returns a &str
, but the operator *
returns a str
. It's more like you are calling *Deref::deref(&s)
, ignoring the implied infinite recursion.
Although "useless" is a bit strong; it would still be useful for types that implement Copy
.
See also:
Note that all of the above is effectively true for Index
and IndexMut
as well.
这篇关于为什么Deref :: deref本身的返回类型是引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!