borrowing

Why did compiler not error on this mutable borrow when there is an immutable borrowed string slice reference still in scope?

本小妞迷上赌 提交于 2019-12-12 10:54:36
问题 I am learning Rust from The Rust Programming Language book available from No Starch Press but ran into an issue where the compiler did not behave as explained in the book in chapter 4 on p. 77. Chapter 4 of the book is discussing ownership, and the example on p. 77 is similar to this without the final println!() in main() (I've also added comments and the function from p. 76 to create an MCVE). I also created a playground. fn main() { let mut s = String::from("Hello world!"); let word = first

How does multiple mutable reference prevention work in Rust?

流过昼夜 提交于 2019-12-11 19:12:57
问题 Why it is allowed to do something like this: fn main() { let mut w = MyStruct; w.fun1(); } struct MyStruct; impl MyStruct { fn fun1(&mut self) { self.fun2(); } fn fun2(&mut self) { println!("Hello world 2"); } } In the above code fun1() gets mut MyStruct and calls fun2() also with mut MyStruct . Is it double mutable reference in one scope? 回答1: This is allowed because the borrow checker can conclude there is only one mutable reference being accessed during execution. While fun2 is running, no

Cannot infer an appropriate lifetime when calling a mutable method with references passed as closure arguments

和自甴很熟 提交于 2019-12-11 04:49:39
问题 I'm trying to make a small game in Rust. I want to use something similar to the entity-component-system pattern to handle all the game objects. My general idea is to have a GameLoop struct which holds all the necessary data to update and draw the game (the screen, a timestamp, ...). The World struct is supposed to hold all the game entities and update them in the dispatch function. It calls all the registered callbacks which are stored in the World struct as well (those are the "systems").

How to build a flexible multiple type data system in Rust without cloning strings?

穿精又带淫゛_ 提交于 2019-12-11 02:04:15
问题 I want to build a system where data of different types ( i32 , String , ...) flows between functions that modify the data. For example, I want to have an add function that gets "some" data and adds it. The add function gets something of type Value and if Value is an i32 , it adds the two i32 values, if it is of type String , it returns a string that combines both strings. I know that this would be almost perfect for template programming (or whatever this is called in Rust, I'm coming from C++

How can I obtain an &A reference from a Rc<RefCell<A>>?

左心房为你撑大大i 提交于 2019-12-11 00:47:48
问题 I have design issue that I would like solve with safe Rust that I haven't been able to find a viable solution. I can't use a RefCell because you can't get a & reference to the data, only Ref / RefMut . Here is a simplified example with irrelevant fields / methods removed use std::cell::RefCell; use std::rc::Rc; struct LibraryStruct {} impl LibraryStruct { fn function(&self, _a: &TraitFromLibrary) {} } trait TraitFromLibrary { fn trait_function(&self, library_struct: LibraryStruct); } // I don

Am I incorrectly implementing IntoIterator for a reference to a LazyList implementation or is this a Rust bug?

泪湿孤枕 提交于 2019-12-10 19:55:35
问题 In implementing a version of a LazyList (an immutable lazily-computed memoized singly-linked list, much as Haskell lists), I have run into a problem of implementing IntoIterator in that the code does not drop the reference when I think it should. The following code has been simplified so as just to show the problem; thus, is not generic and does not include all of the methods not related to implementing IntoIterator : use std::cell::UnsafeCell; use std::mem::replace; use std::rc::Rc; // only

When should I use a reference instead of transferring ownership?

自闭症网瘾萝莉.ら 提交于 2019-12-10 18:03:28
问题 From the Rust book's chapter on ownership, non-copyable values can be passed to functions by either transferring ownership or by using a mutable or immutable reference. When you transfer ownership of a value, it can't be used in the original function anymore: you must return it back if you want to. When you pass a reference, you borrow the value and can still use it. I come from languages where values are immutable by default (Haskell, Idris and the like). As such, I'd probably never think

Why Rust prevents from multiple mutable references?

前提是你 提交于 2019-12-07 20:46:35
问题 Like in the topic, why Rust prevents from multiple mutable references? I have read chapter in rust-book, and I understand that when we have multi-threaded code we are secured from data races but let's look at this code: fn main() { let mut x1 = String::from("hello"); let r1 = &mut x1; let r2 = &mut x1; r1.insert(0, 'w'); } This code is not running simultaneously so there is no possibility for data races. What is more when I am creating new thread and I want to use variable from parent thread

Why Rust prevents from multiple mutable references?

不打扰是莪最后的温柔 提交于 2019-12-06 07:31:18
Like in the topic, why Rust prevents from multiple mutable references? I have read chapter in rust-book, and I understand that when we have multi-threaded code we are secured from data races but let's look at this code: fn main() { let mut x1 = String::from("hello"); let r1 = &mut x1; let r2 = &mut x1; r1.insert(0, 'w'); } This code is not running simultaneously so there is no possibility for data races. What is more when I am creating new thread and I want to use variable from parent thread in a new thread I have to move it so only new thread is an owner of the parent variable. The only

How can multiple threads share an iterator?

可紊 提交于 2019-12-06 06:37:29
I've been working on a function that will copy a bunch of files from a source to a destination using Rust and threads. I'm getting some trouble making the threads share the iterator. I am not still used to the borrowing system: extern crate libc; extern crate num_cpus; use libc::{c_char, size_t}; use std::thread; use std::fs::copy; fn python_str_array_2_str_vec<T, U, V>(_: T, _: U) -> V { unimplemented!() } #[no_mangle] pub extern "C" fn copyFiles( sources: *const *const c_char, destinies: *const *const c_char, array_len: size_t, ) { let src: Vec<&str> = python_str_array_2_str_vec(sources,