gpt4 book ai didi

rust - 将函数指针添加到结构的生命周期问题

转载 作者:行者123 更新时间:2023-11-29 08:23:47 28 4
gpt4 key购买 nike

我正在尝试向结构添加函数指针,但不知道该怎么做。这是一个简单的例子:

struct Ure<'a> {
num: u64,
func: Option<&'a Fn(u64) -> u64>,
}

impl<'a> Ure<'a> {
fn g42_ure(n: u64) -> Ure<'a> {
Ure {
num: n,
func: Some(&Ure::g42),
}
}

fn g42(u: u64) -> u64 {
if u > 42 { u } else { 42 }
}
}

这会导致以下错误:

error: borrowed value does not live long enough
--> <anon>:10:23
|
10 | func: Some(&Ure::g42),
| ^^^^^^^^ does not live long enough
11 | }
12 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the body at 7:34...
--> <anon>:7:35
|
7 | fn g42_ure(n: u64) -> Ure<'a> {
| ___________________________________^ starting here...
8 | | Ure {
9 | | num: n,
10 | | func: Some(&Ure::g42),
11 | | }
12 | | }
| |_____^ ...ending here

有没有办法在这个例子中获得对函数 g42 的引用,使其存活足够长的时间,而无需将其作为参数传递给 g42_ureg42 的定义位置对我来说并不重要(无论是在 impl Ureg42_ure() 还是两者都不在),而是来自一个OOP 背景在 impl Ure 中看起来更整洁。

我刚刚开始学习 Rust(并且非常喜欢它),所以任何帮助将不胜感激,谢谢。

最佳答案

Fn(u64) -> u64 类型不是函数指针,而是特征(-object)。 Rust 中的函数指针写成 fn(u64) -> u64,带有一个小写 f!另请注意,fn(u64) -> u64已经函数指针,无需再说&fn(u64) -> u64!

因此,使其工作的一种方法是使用函数指针 (Playground):

struct Ure {
num: u64,
func: Option<fn(u64) -> u64>,
// ^^^^^^^^^^^^^^
}

impl Ure {
fn g42_ure(n: u64) -> Ure {
Ure {
num: n,
func: Some(Ure::g42),
// ^^^^^^^^
}
}
// ...
}

不过,函数指针有局限性。具体来说,他们不能拥有像闭包那样的环境。这就是 Fn 特性(连同它的兄弟 FnMutFnOnce)对一般可调用事物(包括函数指针和闭包)的抽象).

问题是您不能轻易使用特征对象。可能最简单的方法是使用 Box 在堆上存储和拥有一个特征对象。它看起来像这样(Playground):

struct Ure {
num: u64,
func: Option<Box<Fn(u64) -> u64>>,
// ^^^^^^^^^^^^^^^^^^^
}

impl Ure {
fn g42_ure(n: u64) -> Ure {
Ure {
num: n,
func: Some(Box::new(Ure::g42)),
// ^^^^^^^^^^^^^^^^^^
}
}
// ...
}

关于rust - 将函数指针添加到结构的生命周期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694632/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com