borrow-checker

Why does the compiler tell me to consider using a `let` binding\" when I already am?

▼魔方 西西 提交于 2019-12-03 16:28:58
What is my error and how to fix it? fn get_m() -> Vec<i8> { vec![1, 2, 3] } fn main() { let mut vals = get_m().iter().peekable(); println!("Saw a {:?}", vals.peek()); } ( playground ) The compiler's error suggests "consider using a let binding" — but I already am: error[E0597]: borrowed value does not live long enough --> src/main.rs:6:45 | 6 | let mut vals = get_m().iter().peekable(); | ------- ^ temporary value dropped here while still borrowed | | | temporary value created here 7 | println!("Saw a {:?}", vals.peek()); 8 | } | - temporary value needs to live until here | = note: consider

Is there a way to store a texture inside a struct using rust-sdl2? [duplicate]

流过昼夜 提交于 2019-12-02 20:06:27
问题 This question already has answers here : Cannot infer an appropriate lifetime for autoref due to conflicting requirements (1 answer) Why can't I store a value and a reference to that value in the same struct? (2 answers) Closed 7 months ago . I'm using the rust-sdl2 crate to paint a buffer on a window screen, so I decided to abstract the SDL calls. I tried to follow this example from the rust-sdl2 repo and my first thought was to create something like this: pub struct SDLDisplay { sdl_context

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

守給你的承諾、 提交于 2019-12-02 20:01:15
问题 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();

Is there a way to store a texture inside a struct using rust-sdl2? [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-02 12:19:57
This question already has an answer here: Cannot infer an appropriate lifetime for autoref due to conflicting requirements 1 answer Why can't I store a value and a reference to that value in the same struct? 2 answers I'm using the rust-sdl2 crate to paint a buffer on a window screen, so I decided to abstract the SDL calls. I tried to follow this example from the rust-sdl2 repo and my first thought was to create something like this: pub struct SDLDisplay { sdl_context: Sdl, video_subsystem: VideoSubsystem, canvas: WindowCanvas, texture_creator: TextureCreator<sdl2::video::WindowContext>,

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

柔情痞子 提交于 2019-12-02 09:02:12
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(); Combined(thing, &thing) } Sometimes, I'm not even taking a reference of the value and I get the same

cannot borrow variable as mutable because it is also borrowed as immutable while building a self-referential HashMap

北城余情 提交于 2019-12-02 08:20:34
I'm trying to build a self-referential HashMap : use std::collections::HashMap; struct Node<'a> { byte: u8, map: HashMap<i32, &'a Node<'a>>, } fn main() { let mut network = HashMap::<u32, Node>::new(); network.insert(0, Node { byte: 0, map: HashMap::<i32, &Node>::new() }); network.insert(1, Node { byte: 1, map: HashMap::<i32, &Node>::new() }); let zeroeth_node = network.get(&0).unwrap(); let mut first_node = network.get_mut(&1).unwrap(); first_node.map.insert(-1, zeroeth_node); } I'm running into a borrow-checker error, but I don't understand its source -- is it my method of updating the

How to use struct self in member method closure

我是研究僧i 提交于 2019-12-02 02:48:48
问题 How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url() : fn fetch_access_token(_base_url: &String) -> String { String::new() } fn get_env_url() -> String { String::new() } pub struct App { pub base_url: Option<String>, pub access_token: Option<String>, } impl App { pub fn new() -> App { App { base_url: None, access_token: None, } } pub fn get_base_url(&mut self) -> &String { self.base_url.get_or_insert_with(|| get_env_url()) } pub fn

How to use struct self in member method closure

大兔子大兔子 提交于 2019-12-02 01:01:57
How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url() : fn fetch_access_token(_base_url: &String) -> String { String::new() } fn get_env_url() -> String { String::new() } pub struct App { pub base_url: Option<String>, pub access_token: Option<String>, } impl App { pub fn new() -> App { App { base_url: None, access_token: None, } } pub fn get_base_url(&mut self) -> &String { self.base_url.get_or_insert_with(|| get_env_url()) } pub fn get_access_token(&mut self) -> &String { self.access_token .get_or_insert_with(|| fetch_access_token

Returning a RWLockReadGuard independently from a method

我是研究僧i 提交于 2019-12-01 22:42:37
I have an object of type Arc<RwLock<SessionData>> And I have a method that is supposed to take some kind of reference to SessionData fn some_method(session: ...) I'm using Rocket (a web-framework for Rust), and I can't directly invoke the method, because it is invoked by Rocket. However, I can provide it with an implementation that creates an object that will be passed to the handler. It looks a bit like this: impl<'a, 'r> request::FromRequest<'a, 'r> for SomeType { type Error = (); fn from_request(request: &'a request::Request<'r>) -> request::Outcome<Self, Self::Error> { // return object

How can I convince the borrow checker to allow me to cache values?

走远了吗. 提交于 2019-12-01 21:40:59
The borrow checker beat me: use std::collections::HashMap; struct Cache { cache: Vec<HashMap<String, String>>, } impl Cache { fn get(&mut self, index: usize, key: String) -> String { let mut cache = &mut self.cache[index]; match cache.get(&key) { Some(r) => { return r.clone(); } None => { let r = "foo".to_string(); // something smart here cache.insert(key, r.clone()); return r; } } } } What I get: error[E0502]: cannot borrow `*cache` as mutable because it is also borrowed as immutable --> src/main.rs:16:17 | 10 | match cache.get(&key) { | ----- immutable borrow occurs here ... 16 | cache