I've been fighting the compiler for a while, and I'm not sure what I'm doing wrong. Here is a simplified example:
extern crate byte_stream_splitter;
use byte_stream_splitter::ByteStreamSplitter;
use std::io::{StdinLock};
pub fn msg_stream<'a>(stdin: &'a mut StdinLock<'a>, separator: &'a [u8]) -> ByteStreamSplitter<'a, &'a mut StdinLock<'a>> {
let splitter = ByteStreamSplitter::new(&mut stdin, &separator);
splitter
}
fn main() {
println!("Hello, world!");
}
And here's the error:
error[E0597]: `stdin` does not live long enough
--> src/main.rs:9:55
|
9 | let splitter = ByteStreamSplitter::new(&mut stdin, &separator);
| ^^^^^ borrowed value does not live long enough
...
12 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 6:1...
--> src/main.rs:6:1
|
6 | / pub fn msg_stream<'a>(stdin: &'a mut StdinLock<'a>, separator: &'a [u8]) ->
7 | | ByteStreamSplitter<'a, &'a mut StdinLock<'a>> {
8 | |
9 | | let splitter = ByteStreamSplitter::new(&mut stdin, &separator);
10 | |
11 | | splitter
12 | | }
| |_______^
So, it seems to me this error is saying that the StdinLock reference that is being passed into the function is getting dropped at the end of the msg_stream function, but the ByteStreamSplitter wants to hold onto that same reference. Is that correct?
So what's the idiomatic way of returning an object that contains a borrowed value?