Relative Content

Tag Archive for rustborrow-checker

How to organize blocking method and method with mutable &self in Rust?

let mut peer_poller = Arc::new(Mutex::new(PeerPoller::new()?)); let mut peer_poller_clone = peer_poller.clone(); thread::spawn(move || { loop { let p = rx.recv().unwrap(); match peer_poller_clone.lock() { <—- A Ok(mut g) => { g.add_peer(p); } Err(e) => {} } } }); tx.send(peer) match peer_poller.lock() { <—- B Ok(mut g) => { g.run(); } Err(e) => {} } The run() method […]

Value borrow issue in Rust with bufreader

I’ve searched (and found btw) a lot of issues that are somewhat identical yet I’m struggling to find a way to get code below working in a Rustomatic way. I’ve made an grep equivalant that works, now I wanted to make it better with bufreader instead of loading whole the text(log file actually). But I have an issue with the borrowchecker that I cannot seem to get working with whatever I’m trying. I’ve tried with slices but other errors appear so I’m somewhat lost how I can make this work.

Rust borrow checker in while let loop

I’m very new to rust, attempting to create some ADTs to learn the ropes. I’m kinda stuck on trying to return the popped node from a linked list. As I understand, I’m mutably borrowing current.next in the loop so when I then attempt to take() it, the borrow checker flips out. I tried gpt, copilot and other sources but can’t seem to figure this out. Any tips on how to fix this would be greatly appreciated. Is this even a good approach in rust?

How to fix my code so the HashMap hmap_for_data holds the counts of items in immutable vector aux_vec_i32?

This is a simplified version of code that I written for a leetcode question. In the problem from leetcode I have an immutable input like aux_vec_i32 below and I need to return something more complicated than in the sample code below, but the compiler errors are similar between the leetcode stuff I wrote and what is below. In the end hmap_for_data should hold the counts of unique counts of items in aux_vec_i32.