How to fix lifetime error when function returns a serde Deserialize type?
I found an answer from Serde issue 891: I should use DeserializeOwned instead of Deserialize.
I found an answer from Serde issue 891: I should use DeserializeOwned instead of Deserialize.
In its essence, the Arc and Mutex wrapping is superfluous: you are passing a reference to something on the local stack. When you spawn a thread with std::thread::spawn, there is nothing linking the lifetimes together; the main thread is quite at liberty to conclude and free anything in it—in this case, including a—before any other … Read more
The short version is that there’s a difference between the lifetimes that are inferred if the closure is written inline or stored as a variable. Write the closure inline and remove all the extraneous types: fn test(points: &[Point]) -> (&Point, f32) { let init = points.first().expect(“No initial”); fold(&points, (init, 0.), |(q, max_d), p| { let … Read more
This is 90% a duplicate of Return local String as a slice (&str), see that for multiple other solutions. There’s one extra possibility since this is all in one function: You can declare a variable for the String and only set it when you need to allocate. The compiler (obliquely) suggests this: consider using a … Read more
First, let’s take a look at what lifetime is actually expected. There is an implicit lifetime in the signature of description: fn description(&self) -> &str // Can be rewritten as fn description<‘a>(&’a self) -> &’a str The returned pointer must be valid for at least as long as self. Now consider s. It will hold … Read more
I find the error message pretty straightforward: the trait std::marker::Send is not implemented for Expr + ‘static required because of the requirements on the impl of std::marker::Send for std::sync::Arc<Expr + ‘static> required because it appears within the type Container required because of the requirements on the impl of std::marker::Send for std::sync::Arc<Container> required because it appears … Read more
This is part of the well-formedness rules. The type &’a T is only well-formed if T: ‘a (“T outlives ‘a”; it is required because we have a reference which we can access during the scope ‘a; the pointed-to value in T needs to be valid for at least that scope, too). struct RefWrapper<‘a, T> is … Read more
The problem is that string is created in your function and will be destroyed when the function returns. The vector you want to return contains slices of string, but those will not be valid outside of your function. If you’re not terribly worried about performance, you can return a Vec<String> from your function. You just … Read more
This seems to be a bug in the compiler. You can make the compiler happy by using an explicit return statement: use std::io::{stdin, BufRead}; fn foo() -> usize { let stdin = stdin(); let stdinlock = stdin.lock(); return stdinlock .lines() .count(); } fn main() {} playground As mentioned in the comments, there are multiple Rust … Read more
Is there anything unsafe about having two mutable references to nonoverlapping slices of the same array? There isn’t, but Rust’s type system cannot currently detect that you’re taking mutable references to two non-overlapping parts of a slice. As this is a common use case, Rust provides a safe function to do exactly what you want: … Read more