borrowing

Why is “&&” being used in closure arguments?

佐手、 提交于 2019-11-28 01:03:20
I have two questions regarding this example : let a = [1, 2, 3]; assert_eq!(a.iter().find(|&&x| x == 2), Some(&2)); assert_eq!(a.iter().find(|&&x| x == 5), None); Why is &&x used in the closure arguments rather than just x ? I understand that & is passing a reference to an object, but what does using it twice mean? I don't understand what the documentation says: Because find() takes a reference, and many iterators iterate over references, this leads to a possibly confusing situation where the argument is a double reference. You can see this effect in the examples below, with &&x . Why is Some(

Cannot borrow `*x` as mutable because it is also borrowed as immutable

倖福魔咒の 提交于 2019-11-27 04:53:35
问题 I'm making a Combinatory Optimization project to learn Rust and I've got a problem I cannot resolve myself... I've got 2 functions : pub fn get_pareto_front_offline<'a>(scheduling_jobs: &'a Vec<Vec<u32>>, costs_vector: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> { // ... } and pub fn pareto_approach_offline<'a>(list_of_jobs: &'a mut Vec<Vec<u32>>, neighborhood: &'a mut Vec<Vec<u32>>, costs: &'a Vec<(u32, u32)>) -> Vec<(&'a Vec<u32>, &'a (u32, u32))> { let pareto_front = get

How can I mutate other elements of a HashMap when using the entry pattern?

a 夏天 提交于 2019-11-27 02:24:56
I'd like to use a HashMap to cache an expensive computation that's dependent on other entries in the map. The entry pattern only provides a mutable reference to the matched value, but not to the rest of the HashMap . I'd really appreciate feedback on a better way to solve this (incorrect) toy example: use std::collections::HashMap; use std::collections::hash_map::Entry::{Occupied, Vacant}; fn compute(cache: &mut HashMap<u32, u32>, input: u32) -> u32 { match cache.entry(input) { Vacant(entry) => if input > 2 { // Trivial placeholder for an expensive computation. *entry.insert(compute(&mut cache

Why is “&&” being used in closure arguments?

风格不统一 提交于 2019-11-26 18:31:34
问题 I have two questions regarding this example: let a = [1, 2, 3]; assert_eq!(a.iter().find(|&&x| x == 2), Some(&2)); assert_eq!(a.iter().find(|&&x| x == 5), None); Why is &&x used in the closure arguments rather than just x ? I understand that & is passing a reference to an object, but what does using it twice mean? I don't understand what the documentation says: Because find() takes a reference, and many iterators iterate over references, this leads to a possibly confusing situation where the

Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time

删除回忆录丶 提交于 2019-11-26 00:07:21
问题 I\'m trying to navigate a recursive data structure iteratively in order to insert elements at a certain position. To my limited understanding, this means taking a mutable reference to the root of the structure and successively replacing it by a reference to its follower: type Link = Option<Box<Node>>; struct Node { next: Link } struct Recursive { root: Link } impl Recursive { fn back(&mut self) -> &mut Link { let mut anchor = &mut self.root; while let Some(ref mut node) = *anchor { anchor =

Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?

倖福魔咒の 提交于 2019-11-25 22:56:08
问题 I wrote some Rust code that takes a &String as an argument: fn awesome_greeting(name: &String) { println!(\"Wow, you are awesome, {}!\", name); } I\'ve also written code that takes in a reference to a Vec or Box : fn total_price(prices: &Vec<i32>) -> i32 { prices.iter().sum() } fn is_even(value: &Box<i32>) -> bool { **value % 2 == 0 } However, I received some feedback that doing it like this isn\'t a good idea. Why not? 回答1: TL;DR: One can instead use &str , &[T] or &T to allow for more