AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78947381
Accepted
Aleksander Krauze
Aleksander Krauze
Asked: 2024-09-04 15:41:07 +0800 CST2024-09-04 15:41:07 +0800 CST 2024-09-04 15:41:07 +0800 CST

将函数参数中的 impl Trait 更改为泛型是否会造成重大改变?

  • 772

impl Trait将函数参数类型从 更改为泛型是否可能是一个重大更改? Rust 参考指出:

注意:对于函数参数,泛型类型参数和impl Trait并不完全等价。对于泛型参数(例如)<T: Trait>,调用者可以选择T在调用站点使用GenericArgs明确指定 的泛型参数,例如foo::<usize>(1)。如果impl Trait是任何函数参数的类型,则调用者在调用该函数时永远不能提供任何泛型参数。这包括返回类型的泛型参数或任何 const 泛型。

因此,将函数签名从其中一个更改为另一个可能会对函数调用者造成重大改变。

但是,如果至少有一个impl Trait参数,则调用者不能命名任何泛型参数,这是否意味着更改impl Trait为<T: Trait>只能使调用者能够命名泛型参数?由于泛型已经推断出来,我们也不能破坏类型推断。

编辑:我假设每个都impl Trait更改为不同的、唯一的泛型类型(不与其他impl Traits 或以前的泛型重叠)。违反这一点显然会造成破坏,感谢@啊鹿Dizzyi 指出这一点。

我在这里列出了(我认为是详尽的)impl Trait更改为通用的案例列表。如果其中任何一个案例可能导致下游中断,请说明原因。如果我遗漏了某些案例,请解释它是否导致中断。

  1. 仅更改impl Trait
// before
pub fn foo(_: impl Trait) {}

// after
pub fn foo<T: Trait>(_: T) {}
  1. 很多impl Trait,但只改变其中的一些
// before
pub fn foo(_: impl Trait1, _: impl Trait2, _: impl Trait3) {}

// after
pub fn foo<T1: Trait1, T2: Trait2>(_: T1, _: T2, _: impl Trait3) {}
  1. 很多impl Trait,全部改变
// before
pub fn foo(_: impl Trait1, _: impl Trait2, _: impl Trait3) {}

// after
pub fn foo<T1: Trait1, T2: Trait2, T3: Trait3>(_: T1, _: T2, _: T3) {}
  1. 一些通用类型,改变一些impl Trait,但保留至少一个
// before
pub fn foo<T1: Trait1>(_: T1, _: impl Trait2, _: impl Trait3) {}

// after
pub fn foo<T1: Trait1, T2: Trait2>(_: T1, _: T2, _: impl Trait3) {}
  1. 一些通用类型,改变所有impl Trait的
// before
pub fn foo<T1: Trait1>(_: T1, _: impl Trait2, _: impl Trait3) {}

// after
pub fn foo<T1: Trait1, T2: Trait2, T3: Trait3>(_: T1, _: T2, _: T3) {}
rust
  • 2 2 个回答
  • 23 Views

2 个回答

  • Voted
  1. 啊鹿Dizzyi
    2024-09-04T16:02:52+08:002024-09-04T16:02:52+08:00

    发生这种情况的一种情况是,当您想让函数接受两个参数,并且这两个参数都实现某些特征时Trait,但您不关心这两个参数是否是同一类型。

    因为当你定义一个泛型类型时T,所有出现的内容都会绑定到同一个类型。并且会导致错误,因此,在更改时需要小心。

    例子

    trait Trait {}
    
    fn foo_impl(_: impl Trait, _: impl Trait) {}
    fn foo_generic<T: Trait>(_: T, _: T) {}
    fn foo_generic_ok<T1: Trait, T2: Trait>(_: T1, _: T2) {}
    
    impl Trait for i32 {}
    impl Trait for f32 {}
    
    fn main() {
        foo_impl(1, 1.0);
        foo_generic(1, 1.0); // <-- this will error
        foo_generic_ok(1, 1.0);
    }
    

    错误信息

    error[E0308]: mismatched types
      --> ****\src/main.rs:12:20
       |
    12 |     foo_generic(1, 1.0);
       |     ----------- -  ^^^ expected `i32`, found floating-point number
       |     |           |
       |     |           expected all arguments to be this `i32` type because they need to match the type of this parameter
       |     arguments to this function are incorrect
       |
    note: function defined here
      --> ****\src/main.rs:4:4
       |
    4  | fn foo_generic<T: Trait>(_: T, _: T) {}
       |    ^^^^^^^^^^^ -         ----  ---- this parameter needs to match the `i32` type of {unknown}
       |                |         |
       |                |         {unknown} needs to match the `i32` type of this parameter
       |                {unknown} and {unknown} all reference this parameter T
    
    For more information about this error, try `rustc --explain E0308`.
    
    • 1
  2. Best Answer
    drewtato
    2024-09-04T16:42:19+08:002024-09-04T16:42:19+08:00

    我最近编辑了参考资料的这部分,因为它已经过时了。您可以在夜间版本中看到更新(它应该会在明天(9 月 5 日)稳定发布)。

    简而言之,1.63.0 增加了在存在泛型时指定泛型的可能性impl Trait。据我所知,你说得对,这不可能是一个重大变化。从 1.63.0 开始,如果至少有一个现有的泛型,它就可能是一个重大变化。

    长版本位于PR #1495中。

    • 1

相关问题

  • 在匹配内重用函数时,匹配臂具有预期的不兼容类型

  • match 语句中的 Rust 类型转换

  • 如何强制匹配的返回类型为()?

  • 原始表示中的 Rust 枚举

  • 有没有办法直接简化 Result<String, VarError> 中 Ok("VAL") 的匹配

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve