我想编译以下代码:
struct Provider {}
impl Provider {
fn get_string<'a>(&'a self) -> &'a str { "this is a string" }
}
fn main() {
let provider = Provider{};
let mut vec: Vec<&str> = Vec::new();
// PROBLEM: how do I say that this reference s here
// needs to live as long as vec?
let fun = |s: &str| {
vec.push(s);
};
fun(provider.get_string());
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的编译错误:
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:9:22
|
9 | let …Run Code Online (Sandbox Code Playgroud) 我在尝试编译下面的 Rust 代码时遇到了一对奇怪的错误。在寻找具有类似问题的其他人时,我遇到了另一个问题,该问题具有相同的(看似相反的)错误组合,但无法将解决方案从那里推广到我的问题。
基本上,我似乎忽略了 Rust 所有权系统的一个微妙之处。尝试在这里编译(非常精简的)代码:
struct Point {
x: f32,
y: f32,
}
fn fold<S, T, F>(item: &[S], accum: T, f: F) -> T
where
F: Fn(T, &S) -> T,
{
f(accum, &item[0])
}
fn test<'a>(points: &'a [Point]) -> (&'a Point, f32) {
let md = |(q, max_d): (&Point, f32), p: &'a Point| -> (&Point, f32) {
let d = p.x + p.y; // Standing in for a function call
if d > max_d {
(p, d) …Run Code Online (Sandbox Code Playgroud) 我正在尝试在返回闭包的结构上编写一个方法。此闭包应将&[u8]具有任意生命周期的 a'inner作为参数并返回相同的类型&'inner [u8]。为了执行它的功能,闭包还需要一个对 struct 成员的(共享)引用&self。这是我的代码:
#![warn(clippy::pedantic)]
// placeholder for a large type that I can't afford to clone
struct Opaque(usize);
struct MyStruct<'a>(Vec<&'a Opaque>);
impl<'a> MyStruct<'a> {
pub fn get_func_for_index(
&'a self,
n: usize,
) -> Option<impl for<'inner> Fn(&'inner [u8]) -> &'inner [u8] + 'a> {
// the reference to the struct member, captured by the closure
let opaque: &'a Opaque = *self.0.get(n)?;
Some(move |i: &[u8]| {
// placeholder: do something with the …Run Code Online (Sandbox Code Playgroud) 我想存储一个回调,它可以采用不同类型的参数(拥有的值和引用),还可以修改其环境(因此是 FnMut)。当使用引用调用回调时,我希望编译器强制该参数仅在闭包主体中有效。我尝试使用盒装闭包来实现这一点。
下面显示了一个最小示例:
fn main() {
let mut caller = Caller::new();
let callback = |x: &Foo| println!("{:?}", x);
caller.register(callback);
let foo = Foo{
bar: 1,
baz: 2,
};
//callback(&foo); // works
caller.invoke(&foo); // borrowed value does not live long enough
}
struct Caller<'a, T> {
callback: Box<dyn FnMut(T) + 'a>
}
impl<'a, T> Caller<'a, T> {
fn new() -> Self {
Caller {
callback: Box::new(|_| ()),
}
}
fn register(&mut self, cb: impl FnMut(T) + 'a) {
self.callback = Box::new(cb); …Run Code Online (Sandbox Code Playgroud) 我对下面的生命周期发生了什么感到困惑:
struct Foo{}
impl Foo {
fn foo(&self, _s: &str) {}
}
fn main() {
let foo = &Foo{};
let closure = |s| foo.foo(s);
// Part 1: error message about wrong lifetime in FnOnce
take_closure(closure);
// Part 2: no error when inlined
take_closure(|s| foo.foo(s));
// Part 3: no error when `dyn`d and given explicit signature
let closure: &dyn Fn(&str) -> _ = &|s| foo.foo(s);
take_closure(closure);
}
fn take_closure(f: impl Fn(&str) -> ()) {
let s = get_string();
f(&s)
}
fn …Run Code Online (Sandbox Code Playgroud) 虽然下面的代码是一个早期的原型,并且不要过于认真地考虑我在这个阶段如何实现协议缓冲区,但我无法理解生锈编译器带来的错误信息.
src\main.rs:89:9:89:36错误:类型不匹配解析
for<'r> <[closure src\ma in.rs:75:33: 88:10] as core::ops::FnOnce<(u32, gpb::definitions::WireType, &'r collections::vec::Vec<u8>, usize)>>::Output == usize:期望绑定生存期参数,找到具体生存期[E0271] src\main.rs:89 gpb :: decoding :: read_message(source,field_handler);
即使在阅读了有关lifetimes等的3篇文章章节之后.人.我没有遇到"具体的生命周期"这个术语,因此无法弄清楚这个错误与哪些代码有关.闭包本身,一个或多个参数,返回码?关闭的通过 read_message()?...
main.rs片段
fn from_gpb( source : &Vec<u8>) -> TimeMessage {
fn init_vec_u64( count : usize, init_value : u64) -> Vec<u64> {
let mut result = Vec::<u64>::with_capacity(count);
for i in 0..count {
result.push(init_value);
}
result
}
let mut message_id : u32 = 0;
let mut times_sec = init_vec_u64(4,0u64);
let mut times_usec = init_vec_u64(4,0u64);
let mut max_time_index = …Run Code Online (Sandbox Code Playgroud)