borrow-checker

Borrowed value does not live long enough with a Tokio future

[亡魂溺海] 提交于 2021-01-29 12:13:01
问题 I'm trying to write a simple HTTP server using Rust and tokio. Everything works fine until I want to send the response. The code is the following: use std::fs; use std::sync::Arc; use tokio::net::TcpListener; // 0.1.15 use tokio::prelude::*; fn main() { let addr = "0.0.0.0:8080".parse().unwrap(); let listener = TcpListener::bind(&addr).expect("unable to bind TCP listener"); let incoming = listener.incoming(); let server = incoming .map_err(|e| eprintln!("accept failed = {:?}", e)) .for_each(

How can I extend the lifetime of a temporary variable inside of an iterator adaptor in Rust?

雨燕双飞 提交于 2021-01-29 07:51:09
问题 I have a method make_iter() which creates an Iterator with multiple adapters in Rust, which can be simplified as the following MCVE: fn make_iter(first: &First) -> Box<dyn Iterator<Item = String> + '_> { Box::new(first.make_objects().flat_map(|second| { second .iter() .filter(|third| third.as_str() != "t2") .flat_map(|third| vec![format!("{}.A", third), format!("{}.B", third)].into_iter()) .chain( vec![ format!("{}.A", second.name()), format!("{}.B", second.name()), ] .into_iter(), ) })) }

Is there any way to allow moving a container that has a borrowed element but not dropping it?

我们两清 提交于 2021-01-28 20:59:45
问题 I have this container: use std::ptr::NonNull; struct Container { data: NonNull<u8>, } impl Container { fn new() -> Container { todo!() } fn borrow_some_heap_data<'a>(&'a self) -> &'a u8 { todo!() } fn borrow_some_heap_data_mut<'a>(&'a mut self) -> &'a mut u8 { todo!() } } impl Drop for Container { fn drop(&mut self) { todo!() } } fn main() { let container = Container::new(); let data = container.borrow_some_heap_data(); // or mut { let container = container; // move // This is safe because

Writing a chunk stream to a file asynchronously using hyper

拥有回忆 提交于 2021-01-27 13:01:48
问题 I am trying to create a simple function that downloads a remote file to a local filepath using hyper. I need the file write to be asynchronous as well (in my case I am using tokio_fs for that). Here is the code: View in the playground // Parts of the code were omitted, see the playground for full source code pub fn download_file( uri: Uri, file_location: &Path, ) -> Box<Future<Item = (), Error = DownloadFileError>> { let temp_dir_path = tempfile::tempdir().unwrap().into_path(); let file_name

Mutable borrow in function argument

风流意气都作罢 提交于 2021-01-03 06:41:35
问题 Why doesn't the following code compile (playground): use std::collections::HashMap; fn main() { let mut h: HashMap<u32, u32> = HashMap::new(); h.insert(0, 0); h.insert(1, h.remove(&0).unwrap()); } The borrow checker complains that: error[E0499]: cannot borrow `h` as mutable more than once at a time --> src/main.rs:6:17 | 6 | h.insert(1, h.remove(&0).unwrap()); | - ------ ^ second mutable borrow occurs here | | | | | first borrow later used by call | first mutable borrow occurs here The code

Can you control borrowing a struct vs borrowing a field?

耗尽温柔 提交于 2021-01-02 12:42:09
问题 I'm working on a program involving a struct along these lines: struct App { data: Vec<u8>, overlay: Vec<(usize, Vec<u8>)>, sink: Sink, } In brief the data field holds some bytes and overlay is a series of byte sequences to be inserted at specific indices. The Sink type is unimportant except that it has a function like: impl Sink { fn process<'a>(&mut self, input: Vec<&'a [u8]>) { // ... } } I've implemented an iterator to merge the information from data and overlay for consumption by Sink .

Can you control borrowing a struct vs borrowing a field?

本秂侑毒 提交于 2021-01-02 12:38:47
问题 I'm working on a program involving a struct along these lines: struct App { data: Vec<u8>, overlay: Vec<(usize, Vec<u8>)>, sink: Sink, } In brief the data field holds some bytes and overlay is a series of byte sequences to be inserted at specific indices. The Sink type is unimportant except that it has a function like: impl Sink { fn process<'a>(&mut self, input: Vec<&'a [u8]>) { // ... } } I've implemented an iterator to merge the information from data and overlay for consumption by Sink .

Can you control borrowing a struct vs borrowing a field?

♀尐吖头ヾ 提交于 2021-01-02 12:37:38
问题 I'm working on a program involving a struct along these lines: struct App { data: Vec<u8>, overlay: Vec<(usize, Vec<u8>)>, sink: Sink, } In brief the data field holds some bytes and overlay is a series of byte sequences to be inserted at specific indices. The Sink type is unimportant except that it has a function like: impl Sink { fn process<'a>(&mut self, input: Vec<&'a [u8]>) { // ... } } I've implemented an iterator to merge the information from data and overlay for consumption by Sink .

Why can the Rust compiler break borrowing rules when using Rust 1.31?

北城以北 提交于 2021-01-02 07:17:30
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow

Why can the Rust compiler break borrowing rules when using Rust 1.31?

老子叫甜甜 提交于 2021-01-02 07:15:26
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow