简体   繁体   English

如果在函数内创建引用,如何将泛型类型与需要使用生命周期参数的特征绑定在一起?

[英]How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function?

I want to implement a generic fibonacci function that works with any type implementing Zero , One , and AddAssign . 我想实现一个通用的fibonacci函数,该函数可与实现ZeroOneAddAssign任何类型AddAssign I first implemented a version that works fine, but is specialized for num::BigUint ( see on play.rust-lang.org ). 我首先实现了一个工作正常的版本,但专门用于num::BigUint请参见play.rust-lang.org )。 I than came up with the following generic implementation ( see on play.rust-lang.org ): 然后,我提出了以下通用实现( 请参见play.rust-lang.org ):

extern crate num;

use num::{One, Zero};
use std::mem::swap;
use std::ops::AddAssign;

fn fib<T: Zero + One + AddAssign<&T>>(n: usize) -> T {
    let mut f0 = Zero::zero();
    let mut f1 = One::one();
    for _ in 0..n {
        f0 += &f1;
        swap(&mut f0, &mut f1);
    }
    f0
}

This doesn't compile: 这不会编译:

error[E0106]: missing lifetime specifier
 --> src/main.rs:7:34
  |
7 | fn fib<T: Zero + One + AddAssign<&T>>(n: usize) -> T {
  |                                  ^ expected lifetime parameter

Rust wants me to add a lifetime parameter to AddAssign<&T> but I don't know how to express the lifetime of f1 . Rust希望我向AddAssign<&T>添加一个生命周期参数,但是我不知道如何表达f1的生命周期。

You need to use Higher Rank Trait Bounds . 您需要使用较高等级特质界限 This one means basically "For any lifetime 'a , T satisfies the AddAssign<&'a T> trait": 这基本上意味着“对于任何生命'aT满足AddAssign<&'a T>特性”:

fn fib<T>(n: usize) -> T
where
    for<'a> T: Zero + One + AddAssign<&'a T>,

I also had to change the way fib is called because the compiler couldn't figure out the return type, which could be literally any type that implements those traits. 我还必须更改fib的调用方式,因为编译器无法弄清楚返回类型,返回类型实际上可以是实现这些特征的任何类型。 Declaring x 's type gives sufficient context to the compiler so that it knows what you want. 声明x的类型为编译器提供了足够的上下文,以便它知道您想要什么。

fn main() {
    let x: num::BigUint = fib(10);
    // let x = fib::<BigUint>(10); // Also works
    println!("fib(10) = {}", x);
}

playground 操场

暂无
暂无

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

相关问题 如何在另一个泛型类型的特征绑定的类型参数上表达特征? - How can I express a trait bound on a type parameter for another generic type's trait bound? 使用泛型关联类型时,如何指示类型参数的生命周期? - How do I indicate a lifetime bound on a type parameter when using generic associated types? 如何使用使用生命周期名称参数化的特征绑定声明泛型函数? - How to declare a generic function with a trait bound that is parameterized with a lifetime name? 如何使用类型列表的参数创建泛型类型的实例 - How do I create an instance of a generic type with a parameter of type list 我应该如何使用一个受我的特征绑定的通用参数为我的结构实现 From/Into? - How should I implement From/Into for my struct with one generic parameter bound by my trait? 如何创建泛型子类的实例? 获取错误:“绑定不匹配:类型...不是有界参数的有效替代...” - How do I make an instance of generic subclass? Getting error: “Bound mismatch: The type … is not a valid substitute for the bounded parameter …” 如何使用生命周期参数指定关联类型? - How do I specify an associated type with a lifetime parameter? 如何推断此泛型类型的上限? - How do I infer an upper bound for this generic type? Scala:如何设置通用特征? - Scala: How do I set a generic Trait? 如何在特征本身上写一个特征绑定来引用关联类型? - How to write a trait bound for a reference to an associated type on the trait itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM