gpt4 book ai didi

rust - 返回值的Rust生命周期问题

转载 作者:行者123 更新时间:2023-12-03 11:46:30 25 4
gpt4 key购买 nike

对于以下问题,我们深表歉意。编译器说,将创建一个临时文件,该临时文件在仍在使用时将被释放。
我试图做的是Rust的一个示例,它位于https://doc.rust-lang.org/book/ch13-01-closures.html上,在该示例中,如果未找到搜索到的 key ,则Closure应该更新HashMap。
如何使此代码可编译? Playground:
每次我编辑代码时,都会出现新的错误,即使遵循编译器的建议也是如此。

use std::thread;
use std::time::Duration;
use std::collections::HashMap;

fn main() {
let simulated_user_specified_value = 10;
let simulated_random_number = 7;
generate_workout(simulated_user_specified_value, simulated_random_number);

let simulated_user_specified_value = 10;
let simulated_random_number = 7;
generate_workout(simulated_user_specified_value, simulated_random_number);

let simulated_user_specified_value = 50;
let simulated_random_number = 12;
generate_workout(simulated_user_specified_value, simulated_random_number);
}

struct Cacher<'a, T>
where
T: Fn(u32) -> u32,
{
calculation: T,
value: HashMap<&'a u32, &'a u32>,
}

impl<'a, T> Cacher<'a, T>
where
T: Fn(u32) -> u32,
{
fn new(calculation: T) -> Cacher<'a, T> {
Cacher {
calculation,
value: HashMap::new(),
}
}

fn value(&mut self, arg: &'a u32) -> &'a u32 {
println!("{:?}", arg);
self.value.entry(&arg).or_insert(&(self.calculation)(30))
}
}

fn generate_workout(intensity: u32, random_number: u32) {
let mut expensive_result = Cacher::new(|num| -> u32 {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
num
});

if intensity < 25 {
println!("Today, do {} pushups!", expensive_result.value(&intensity));
println!("Next, do {} situps!", expensive_result.value(&intensity));
} else {
if random_number == 3 {
println!("Take a break today! Remember to stay hydrated!");
} else {
println!(
"Today, run for {} minutes!",
expensive_result.value(&intensity)
);
}
}
}
先感谢您,

最佳答案

绝对没有理由将对u32的引用存储在HashMap中。只需存储值本身,您的问题就应该消失。这样做可以使一切编译正常:

use std::thread;
use std::time::Duration;
use std::collections::HashMap;

fn main() {
let simulated_user_specified_value = 10;
let simulated_random_number = 7;
generate_workout(simulated_user_specified_value, simulated_random_number);

let simulated_user_specified_value = 10;
let simulated_random_number = 7;
generate_workout(simulated_user_specified_value, simulated_random_number);

let simulated_user_specified_value = 50;
let simulated_random_number = 12;
generate_workout(simulated_user_specified_value, simulated_random_number);
}

struct Cacher<T>
where
T: Fn(u32) -> u32,
{
calculation: T,
value: HashMap<u32, u32>,
}

impl<T> Cacher<T>
where
T: Fn(u32) -> u32,
{
fn new(calculation: T) -> Cacher<T> {
Cacher {
calculation,
value: HashMap::new(),
}
}

fn value(&mut self, arg: u32) -> u32 {
println!("{:?}", arg);
*self.value.entry(arg).or_insert((self.calculation)(30))
}
}

fn generate_workout(intensity: u32, random_number: u32) {
let mut expensive_result = Cacher::new(|num| -> u32 {
println!("calculating slowly...");
thread::sleep(Duration::from_secs(2));
num
});

if intensity < 25 {
println!("Today, do {} pushups!", expensive_result.value(intensity));
println!("Next, do {} situps!", expensive_result.value(intensity));
} else {
if random_number == 3 {
println!("Take a break today! Remember to stay hydrated!");
} else {
println!(
"Today, run for {} minutes!",
expensive_result.value(intensity)
);
}
}
}

关于rust - 返回值的Rust生命周期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66428961/

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