&* 组合在一起在 Rust 中有什么作用?

编程入门 行业动态 更新时间:2024-10-18 06:06:08
本文介绍了&* 组合在一起在 Rust 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在阅读有关 String 的书籍部分,发现它们使用 &* 组合在一起来转换一段文本.内容如下:

I was reading through the book section about Strings and found they were using &* combined together to convert a piece of text. The following is what it says:

use std::net::TcpStream; TcpStream::connect("192.168.0.1:3000"); // Parameter is of type &str. let addr_string = "192.168.0.1:3000".to_string(); TcpStream::connect(&*addr_string); // Convert `addr_string` to &str.

换句话说,他们说他们正在将 String 转换为 &str.但是,为什么要使用上述两个符号进行转换?这不应该使用其他方法来完成吗?& 不是表示我们正在获取它的引用,然后使用 * 取消引用它吗?

In other words, they are saying they are converting a String to a &str. But why is that conversion done using both of the aforementioned signs? Should this not be done using some other method? Does not the & mean we are taking its reference, then using the * to dereference it?

推荐答案

简而言之:* 触发一个显式的 deref,可以通过 ops::Deref.

In short: the * triggers an explicit deref, which can be overloaded via ops::Deref.

看看这段代码:

let s = "hi".to_string(); // : String let a = &s;

a 的类型是什么?它只是&String!这并不奇怪,因为我们引用了 String.好的,但是这个呢?

What's the type of a? It's simply &String! This shouldn't be very surprising, since we take the reference of a String. Ok, but what about this?

let s = "hi".to_string(); // : String let b = &*s; // equivalent to `&(*s)`

b 的类型是什么?它是 &str!哇,怎么了?

What's the type of b? It's &str! Wow, what happened?

注意 *s 先执行.与大多数运算符一样,解引用运算符 * 也是可重载的,运算符的使用可以被视为 *std::ops::Deref::deref(&s)(请注意,我们在这里递归地取消引用!).String 确实重载这个运算符:

Note that *s is executed first. As most operators, the dereference operator * is also overloadable and the usage of the operator can be considered syntax sugar for *std::ops::Deref::deref(&s) (note that we recursively dereferencing here!). String does overload this operator:

impl Deref for String { type Target = str; fn deref(&self) -> &str { ... } }

所以,*s实际上是*std::ops::Deref::deref(&s),其中deref() 函数具有返回类型 &str 然后再次取消引用.因此,*s 的类型为 str(注意缺少 &).

So, *s is actually *std::ops::Deref::deref(&s), in which the deref() function has the return type &str which is then dereferenced again. Thus, *s has the type str (note the lack of &).

由于 str 未定义大小并且本身不是很方便,我们希望改为引用它,即 &str.我们可以通过在表达式前面添加一个 & 来做到这一点!Tada,现在我们到达了类型 &str!

Since str is unsized and not very handy on its own, we'd like to have a reference to it instead, namely &str. We can do this by adding a & in front of the expression! Tada, now we reached the type &str!

&*s 更像是手动和明确的形式.通常,Deref-overload 是通过自动 deref 强制使用的.当目标类型固定后,编译器会为你解引用:

&*s is rather the manual and explicit form. Often, the Deref-overload is used via automatic deref coercion. When the target type is fixed, the compiler will deref for you:

fn takes_string_slice(_: &str) {} let s = "hi".to_string(); // : String takes_string_slice(&s); // this works!

更多推荐

&* 组合在一起在 Rust 中有什么作用?

本文发布于:2023-11-28 07:07:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   中有   作用   amp   Rust

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!