borrow-checker

Why can't I store a value and a reference to that value in the same struct?

我们两清 提交于 2021-02-11 13:30:04
问题 I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and I want to store that value and a reference to that value in the same structure: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new();

error[E0106]: missing lifetime specifier (despite it being set) [duplicate]

早过忘川 提交于 2021-02-11 00:20:46
问题 This question already has answers here : Contradictory “missing lifetime specifier” error on owned value [duplicate] (1 answer) Getting 'Missing Lifetime specifier' error (1 answer) Why do I get “missing lifetime specifier” or “wrong number of type arguments” when implementing a trait for a struct? (1 answer) Closed 2 years ago . Consider the following code: extern crate clap; use clap::{App}; use std::io; fn parse_argv() -> &'static clap::ArgMatches { return App::new("example") .get_matches(

error[E0106]: missing lifetime specifier (despite it being set) [duplicate]

谁说我不能喝 提交于 2021-02-11 00:20:28
问题 This question already has answers here : Contradictory “missing lifetime specifier” error on owned value [duplicate] (1 answer) Getting 'Missing Lifetime specifier' error (1 answer) Why do I get “missing lifetime specifier” or “wrong number of type arguments” when implementing a trait for a struct? (1 answer) Closed 2 years ago . Consider the following code: extern crate clap; use clap::{App}; use std::io; fn parse_argv() -> &'static clap::ArgMatches { return App::new("example") .get_matches(

Cannot borrow as mutable more than once error in a loop

大城市里の小女人 提交于 2021-02-08 08:58:40
问题 I am working on leetcode problem #83 "Remove Duplicates from Sorted List", but I'm stuck on this borrow checker issue. The ListNode struct is given by the problem so it cannot be changed. I have tried restructuring the loop and if statement, but I haven't found a working solution. What I am trying to do: // Definition for singly-linked list. #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next: Option<Box<ListNode>>, } impl ListNode { #[inline] fn new(val: i32) -> Self

Lifetime issues with a closure argument in Rust

佐手、 提交于 2021-02-08 06:53:21
问题 I'm getting an error when trying to use a closure that does exactly the same as the print function below (in ln.9) The error is the usual borrowed value does not live long enough . I've tried to replicate this in the playground but I can't. I'm certain that this is mainly because I don't really understand what's going on here so any help would be really appreciated. What I can't understand is what is the difference between calling the print function and calling the check closure. They have

How to mutate another item in a vector, but not the vector itself, while iterating over the vector?

穿精又带淫゛_ 提交于 2021-02-07 18:17:44
问题 It is quite clear to me that iterating over a vector shouldn't let the loop body mutate the vector arbitrarily. This prevents iterator invalidation, which is prone to bugs. However, not all kinds of mutation lead to iterator invalidation. See the following example: let mut my_vec: Vec<Vec<i32>> = vec![vec![1,2], vec![3,4], vec![5,6]]; for inner in my_vec.iter_mut() { // <- or .iter() // ... my_vec[some_index].push(inner[0]); // <-- ERROR } Such a mutation does not invalidate the iterator of

Why does the borrow checker disallow a second mutable borrow even if the first one is already out of scope?

你。 提交于 2021-02-07 08:12:36
问题 Background I know that the borrow checker disallows more than one mutable borrows. For example, the code below is invalid: fn main() { let mut x = 42; let a = &mut x; let b = &mut x; println!("{} {}", a, b); } However, if the first borrow is dropped due to out of scope, the second borrow is valid: fn main() { let mut x = 1; { let a = &mut x; println!("{}", a); } let b = &mut x; println!("{}", b); } Because of non-lexical lifetimes (NLL), the first borrow doesn't even have to be out of scope —

Why does the borrow checker disallow a second mutable borrow even if the first one is already out of scope?

ⅰ亾dé卋堺 提交于 2021-02-07 08:11:38
问题 Background I know that the borrow checker disallows more than one mutable borrows. For example, the code below is invalid: fn main() { let mut x = 42; let a = &mut x; let b = &mut x; println!("{} {}", a, b); } However, if the first borrow is dropped due to out of scope, the second borrow is valid: fn main() { let mut x = 1; { let a = &mut x; println!("{}", a); } let b = &mut x; println!("{}", b); } Because of non-lexical lifetimes (NLL), the first borrow doesn't even have to be out of scope —

Why is there a borrow of a moved value when calling a method that takes self by value with an argument that also calls a method?

断了今生、忘了曾经 提交于 2021-02-04 17:47:20
问题 I ran into an issue that forces me to split a nice oneliner into a {} block with an intermediate let . The reason for this isn't clear to me at all. I was able to isolate the issue in this minimal example: struct AB { a: u8, b: u8, } impl AB { fn foo(&self) -> String { String::from("foo") } fn bar(self, x: String) -> String { format!("{} - {} - {}!", x, self.a, self.b) } } fn main() { let x = AB { a: 3, b: 5 }; let result = x.bar(x.foo()); println!("{}", result); } I was under the impression

Borrowed value does not live long enough with a Tokio future

∥☆過路亽.° 提交于 2021-01-29 12:53:30
问题 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(