简体   繁体   English

right = |_a| 的 Rust 泛型类型实现 |b| 乙

[英]Rust generic type implementation for right = |_a| |b| b

In TypeScript, I do:在打字稿中,我这样做:

const right = <T>(a: T) => <U>(b: U) => b;

const log = (msg: unknown) =>
  right
    (console.log(msg))
    (msg);

log(1); // 1
log("hello"); // "hello"
log(right(1)(2)); // 2

Now, I want to implement the same code in Rust with generic types.现在,我想用泛型类型在 Rust 中实现相同的代码。

let right = |_a| |b| b;

let log = |msg| right(println!("{:?}", msg))(msg);

log(1); // 1   
log("hello"); // compile error  
log(right(1)(2)); // compile error

So far, the compile error is like this, and I know why and how the error occurs, and I know this is not a code using generic.到目前为止,编译错误是这样的,我知道错误发生的原因和方式,并且我知道这不是使用泛型的代码。

So my question is what would be a proper code to implement this in Rust using generic types to closure ?所以我的问题是什么是正确的代码拉斯特来实现这一点使用泛型类型关闭

The problem here is that your closures are not generic.这里的问题是你的闭包不是通用的。 They are in fact fixed to a specific type and those types are inferred.它们实际上固定为特定类型,并且这些类型是推断出来的。 The compiler assigns them once with your first use, and then they don't match for subsequent uses.编译器在您第一次使用时分配它们一次,然后它们不匹配以供后续使用。

If you want to use generic types, it's going to be better to use an explicit function instead of a closure:如果要使用泛型类型,最好使用显式函数而不是闭包:

fn right<T, U>(_x: T) -> impl Fn(U) -> U {
    |b| b
}

fn log<T: std::fmt::Debug>(msg: T) {
    right(println!("{:?}", msg))(msg);
}

fn main() {
    log(1); // 1
    log("hello"); // hello
    log(right(1)(2)); // 2
}

Those functions are explicitly generic and appropriately typed, so they'll be monomorphized as appropriate for each instantiation.这些函数是明确的通用和适当的类型,因此它们将根据每个实例化进行单态化。 If you're unhappy with the increased scope of those functions, you can push them into main , and they'll be visible only from there, just like your closures.如果您对这些函数的范围增加不满意,您可以将它们推入main ,并且它们只能从那里可见,就像您的闭包一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型'A | B' 不可分配给类型 'A &amp; B' - Type 'A | B' is not assignable to type 'A & B' 强制Typescript个泛型参数(a:T,B:T)为同一类型 - Force Typescript generic parameters (a: T, B: T) to be of the same type “A 可分配给类型 B 的约束,但 B 可以用不同的子类型实例化”在通用组件中 - "A is assignable to the constraint of type B but B could be instanciated with a different subtype" in generic component 打字稿类型 a 或类型 b - Typescript type a or type b 通用类型:动态类型检查特定 object 属性:在通用类型 B 中使用来自类型 A 的键~值对 - Generic Types: Dynamically type check specific object properties: using key ~ value pair from Type A, in Generic Type B 打字稿类型a不可分配给类型b - Typescript type a is not assignable to type b 断言类型 A 可分配给类型 B - assert that type A is assignable to type B Typescript 接口 Generics:仅当 T 属于联合类型 'A | 时才允许泛型 T 乙' - Typescript interface Generics : Allow generic T only if T is of union type 'A | B' 输入 &#39;(foo: SomeType) =&gt; &quot;a&quot; | &quot;b&quot;&#39; 不能分配给类型 &#39;SomeType&#39;。 输入 &#39;(foo: SomeType) =&gt; &quot;a&quot; | &quot;b&quot;&#39; 不能分配给类型 &#39;&quot;b&quot;&#39; - Type '(foo: SomeType) => "a" | "b"' is not assignable to type 'SomeType'. Type '(foo: SomeType) => "a" | "b"' is not assignable to type '"b"' 类型 A 缺少类型 B 的以下属性:a、b - Type A is missing the following properties from type B: a, b
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM