gpt4 book ai didi

rust - 将对变量的引用插入到 HashSet 中,然后再使用变量

转载 作者:行者123 更新时间:2023-11-29 08:26:50 25 4
gpt4 key购买 nike

<分区>

在检查变量对是否已经存在后,我正在尝试将变量对插入 HashSet。然后我需要在相同的上下文中对这对进行下游工作。这是一个重现我的问题的 Playground :

https://play.rust-lang.org/?gist=5eecfa9c1e54b468d79955aab916dd84&version=stable&mode=debug&edition=2015

use std::collections::HashSet;

fn main() {
let mut h = HashSet::new();
let a = 1;
let b = 2;
if h.contains(&(&a, &b)) {
println!("fail");
}
h.insert(&(&a, &b));
}
error[E0597]: borrowed value does not live long enough
--> src/main.rs:10:15
|
10 | h.insert(&(&a, &b));
| ^^^^^^^^ - temporary value dropped here while still borrowed
| |
| temporary value does not live long enough
11 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime

error[E0597]: `a` does not live long enough
--> src/main.rs:10:17
|
10 | h.insert(&(&a, &b));
| ^ borrowed value does not live long enough
11 | }
| - `a` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

error[E0597]: `b` does not live long enough
--> src/main.rs:10:21
|
10 | h.insert(&(&a, &b));
| ^ borrowed value does not live long enough
11 | }
| - `b` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

如何检查 ab 是否在集合中,如果不在则插入它们,然后用它们做其他事情?如果我在同一范围内加载对它们的引用,它们是如何被借用的?

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