하나의 매개 변수와 동일한 수명의 연결된 유형을 반환하는 함수로 특성을 정의하려고합니다. (작동하지 않습니다 lifetime parameter not allowed on this type [Self::Output]
를) 다음과 같은하나의 매개 변수와 같은 수명의 연결된 유형을 반환하는 함수로 특성 정의
개념적으로 뭔가 내가 스택 오버플로 인터넷하지만 없음에 관련된 유형의 수명에 대한 몇 가지 질문을 발견
trait Trait {
type Output;
fn get_output<'a>(&self, input: &'a i32) -> Self::Output<'a>;
}
도움이 보인다. 일부는 전체 특성에 수명을 정의 제안 :
trait Trait<'a> {
type Output;
fn get_output(&self, input: &'a i32) -> Self::Output;
}
을하지만이 작업 중 하나를하지 않습니다 : 그것은 컴파일하지만 다음 함수가 컴파일에 실패 :
fn f<'a, T>(var: &T)
where T: Trait<'a>
{
let input = 0i32;
let output = var.get_output(&input);
}
오류를주는 :
를error: `input` does not live long enough
--> <anon>:9:35
|
| let output = var.get_output(&input);
| ^^^^^ does not live long enough
| }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 7:48...
--> <anon>:7:49
|
| fn f<'a, T>(var : &T) where T : Trait<'a> {
| _________________________________________________^ starting here...
| | let input = 0i32;
| | let output = var.get_output(&input);
| | }
| |_^ ...ending here
원하는 방식으로 특성을 정의해야합니까?