rust - 当特征和结构使用相同的方法名时如何调用方法?

标签 rust

这个程序死于无限递归:

use std::any::Any;

trait Foo {
    fn get(&self, index: usize) -> Option<&Any>;
}

impl Foo for Vec<i32> {
    fn get(&self, index: usize) -> Option<&Any> {
        Vec::get(self, index).map(|v| v as &Any)
    }
}

fn main() {
    let v: Vec<i32> = vec![1, 2, 4];
    println!("Results: {:?}", v.get(0))
}

编译器本身对此发出警告:

warning: function cannot return without recurring
  --> src/main.rs:8:5
   |
8  |       fn get(&self, index: usize) -> Option<&Any> {
   |  _____^ starting here...
9  | |         Vec::get(self, index).map(|v| v as &Any)
10 | |     }
   | |_____^ ...ending here
   |
   = note: #[warn(unconditional_recursion)] on by default
note: recursive call site
  --> src/main.rs:9:9
   |
9  |         Vec::get(self, index).map(|v| v as &Any)
   |         ^^^^^^^^^^^^^^^^^^^^^
   = help: a `loop` may express intention better if this is on purpose

为什么通用调用语法在这种情况下不起作用?编译器不理解我想调用 Vec::get 而不是 Foo::get

如果我不想更改函数名称,我该如何解决这个问题?

最佳答案

要指定调用哪个方法,无论是固有的还是由特征提供的,您需要使用 fully qualified syntax :

Type::function(maybe_self, needed_arguments, more_arguments)
Trait::function(maybe_self, needed_arguments, more_arguments)

您的案例不适用,因为 Vec 没有名为 get 的方法! get Deref implementation to [T] 提供.

最简单的解决方法是调用 as_slice直接:

self.as_slice().get(index).map(|v| v as &Any)

您还可以使用在这种情况下需要尖括号的完全限定语法 (<...>) 以避免在声明数组文字时出现歧义:

<[i32]>::get(self, index).map(|v| v as &Any)

universal call syntax

请注意,虽然 Rust 最初使用术语通用函数调用语法 (UFCS),但该术语的用法与现有的理解编程术语冲突,因此不建议使用它。替换术语是完全限定的语法。

关于rust - 当特征和结构使用相同的方法名时如何调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44445730/

相关文章:

rust - 将未知类型的 PgRow 值转换为字符串

rust - UFCS:解析 T::方法

rust - 如何根据运行时信息有条件地跳过测试?

rust - 如何在特征的默认方法中使用调试格式化程序打印 `self`?

rust - 如果可能,实现 copy 的类型是否会被移动?

rust - 如何迭代和过滤数组?

loops - 尝试在循环中更新 Option<&str> 时获取 "temporary value dropped while borrowed"

vector - 在 Rust 中连接向量的最佳方法是什么?

generics - 是否可以为除了一个类型子集以外的所有类型都可以使用的特征创建通用的impl?

rust - 我什么时候需要在 Rust 中指定明确的生命周期?