borrowing

How to wrap a borrowed value in a newtype that is also a borrowed value?

。_饼干妹妹 提交于 2020-12-21 02:54:45
问题 I am trying to use the newtype pattern to wrap a pre-existing type. That inner type has a modify method which lets us work with a borrowed mutable value in a callback: struct Val; struct Inner(Val); impl Inner { fn modify<F>(&self, f: F) where F: FnOnce(&mut Val) -> &mut Val { … } } Now I want to provide a very similar method on my newtype Outer , which however should not work on Val s but again a newtype wrapper WrappedVal : struct Outer(Inner); struct WrappedVal(Val); impl Outer { fn modify

Temporary value dropped while borrowed, but I don't want to do a let

橙三吉。 提交于 2020-12-21 02:42:13
问题 I'm doing something like this: fn main() { //[1, 0, 0, 0, 99]; // return [2, 0, 0, 0, 99] //[2, 3, 0, 3, 99]; // return [2,3,0,6,99] //[2, 4, 4, 5, 99, 0]; // return [2,4,4,5,99,9801] //[1, 1, 1, 4, 99, 5, 6, 0, 99]; // return [30,1,1,4,2,5,6,0,99] let map: Vec<(&mut [usize], &[usize])> = vec![(&mut [1, 0, 0, 0, 99], &[2, 0, 0, 0, 99])]; for (x, y) in map { execute_program(x); assert_eq!(x, y); } } pub fn execute_program(vec: &mut [usize]) { //do something inside vec } Here the playground The

Why is iterating over a collection via `for` loop considered a “move” in Rust?

∥☆過路亽.° 提交于 2020-06-21 05:35:51
问题 I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `v` --> src\main.rs:7:22 | 2 | let v = vec![100, 32, 57]; | - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait 3 | for i in v { | - | | | value moved here | help: consider borrowing to avoid moving into the for loop: `&v` ... 7 | println!("{:?}", v); | ^ value

Why is iterating over a collection via `for` loop considered a “move” in Rust?

家住魔仙堡 提交于 2020-06-21 05:34:58
问题 I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `v` --> src\main.rs:7:22 | 2 | let v = vec![100, 32, 57]; | - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait 3 | for i in v { | - | | | value moved here | help: consider borrowing to avoid moving into the for loop: `&v` ... 7 | println!("{:?}", v); | ^ value

How does assigning to a borrowed variable violate the rules of references?

别来无恙 提交于 2020-05-31 06:31:08
问题 I have this code: struct Foo<'a> { link: &'a i32, } fn main() { let mut x = 33; println!("x:{}", x); let ff = Foo { link: &x }; x = 22; } Which generates this compiler error: error[E0506]: cannot assign to `x` because it is borrowed --> src/main.rs:9:5 | 8 | let ff = Foo { link: &x }; | - borrow of `x` occurs here 9 | x = 22; | ^^^^^^ assignment to borrowed `x` occurs here The Rust book has only two rules: one or more references ( &T ) to a resource, exactly one mutable reference ( &mut T ).

How do I create a struct of references to traits when one object might implement multiple of the traits?

╄→гoц情女王★ 提交于 2020-05-31 04:13:12
问题 I have a struct which manages several sensors. I have a gyroscope, accelerometer, magnetometer, barometer, and thermometer. All of which are traits. pub struct SensorManager { barometer: Barometer + Sized, thermometer: Thermometer + Sized, gyroscope: Gyroscope + Sized, accelerometer: Accelerometer + Sized, magnetometer: Magnetometer + Sized } I need to make it modular so in the configuration file you can specify which sensors you are using. The problem is that some of the sensors overlap. For

How do intertwined scopes create a “data race”? [duplicate]

孤街醉人 提交于 2020-05-28 06:59:20
问题 This question already has answers here : Why does Rust disallow mutable aliasing? (3 answers) Closed 10 days ago . The Rust book talks about having multiple readers and multiple mutable references to an object as a data race situation that may lead to issues. For example, this code: fn main() { let mut x = 1; let r1 = &mut x; *r1 = 2; let r2 = &mut x; *r2 = 3; println!("{}", r1); println!("{}", r2); } will be rejected by Rust compiler because r1 and r2 scopes are intertwined. But what is

How to accept &str, String and &String in a single function?

橙三吉。 提交于 2020-05-25 06:35:18
问题 I want to write a single function, that accepts a &str , a String and a borrowed &String . I've written the following 2 functions: fn accept_str_and_ref_string(value: &str) { println!("value: {}", value); } fn accept_str_and_string<S: Into<String>>(value: S) { let string_value: String = value.into(); println!("string_value: {}", string_value); } fn main() { let str_foo = "foo"; let string_foo = String::from("foo"); accept_str_and_ref_string(str_foo); accept_str_and_ref_string(&string_foo);