borrowing

Iterating through a recursive structure using mutable references and returning the last valid reference

限于喜欢 提交于 2019-12-02 02:01:27
问题 I'm trying to recurse down a structure of nodes, modifying them, and then returning the last Node that I get to. I solved the problems with mutable references in the loop using an example in the non-lexical lifetimes RFC. If I try to return the mutable reference to the last Node , I get a use of moved value error: #[derive(Debug)] struct Node { children: Vec<Node>, } impl Node { fn new(children: Vec<Self>) -> Self { Self { children } } fn get_last(&mut self) -> Option<&mut Node> { self

How to implement prepend for a linked list without needing to assign to a new variable?

匆匆过客 提交于 2019-12-02 01:30:44
Something told me how to implement a linked list: enum List { Cons(u32, Box<List>), Nil, } impl List { fn prepend(self, elem: u32) -> List { Cons(elem, Box::new(self)) } } When I want to use prepend , I need to do the following: list = list.prepend(1); However, I want to create a function that does not need to create a new variable every time prepend returns. I just want to change the list variable itself using prepend : list.prepend(1); Here is one implementation that I come up with, but it's not right: fn my_prepend(&mut self, elem: u32) { *self = Cons(elem, Box::new(*self)); } The error is:

Iterating through a recursive structure using mutable references and returning the last valid reference

﹥>﹥吖頭↗ 提交于 2019-12-01 23:02:58
I'm trying to recurse down a structure of nodes, modifying them, and then returning the last Node that I get to. I solved the problems with mutable references in the loop using an example in the non-lexical lifetimes RFC . If I try to return the mutable reference to the last Node , I get a use of moved value error: #[derive(Debug)] struct Node { children: Vec<Node>, } impl Node { fn new(children: Vec<Self>) -> Self { Self { children } } fn get_last(&mut self) -> Option<&mut Node> { self.children.last_mut() } } fn main() { let mut root = Node::new(vec![Node::new(vec![])]); let current = &mut

In what scenarios are APIs that don't borrow preferred?

谁说我不能喝 提交于 2019-12-01 15:56:18
Rust has the concepts of ownership and borrowing. If a function doesn't borrow its parameter as a reference, the arguments to that function are moved and will be deallocated once they go out of scope. Take this function: fn build_user(email: String, username: String) -> User { User { email: email, username: username, } } This function can be called as: let email = String::from("foo@example.com"); let username = String::from("username"); let user = build_user(email, username); Since email and username have been moved, they can no longer be used after build_user was called. This can be fixed by

In what scenarios are APIs that don't borrow preferred?

南楼画角 提交于 2019-12-01 14:43:58
问题 Rust has the concepts of ownership and borrowing. If a function doesn't borrow its parameter as a reference, the arguments to that function are moved and will be deallocated once they go out of scope. Take this function: fn build_user(email: String, username: String) -> User { User { email: email, username: username, } } This function can be called as: let email = String::from("foo@example.com"); let username = String::from("username"); let user = build_user(email, username); Since email and

How do I efficiently build a vector and an index of that vector while processing a data stream?

痞子三分冷 提交于 2019-11-30 14:00:46
I have a struct Foo : struct Foo { v: String, // Other data not important for the question } I want to handle a data stream and save the result into Vec<Foo> and also create an index for this Vec<Foo> on the field Foo::v . I want to use a HashMap<&str, usize> for the index, where the keys will be &Foo::v and the value is the position in the Vec<Foo> , but I'm open to other suggestions. I want to do the data stream handling as fast as possible, which requires not doing obvious things twice. For example, I want to: allocate a String only once per one data stream reading not search the index

Cannot infer an appropriate lifetime for a closure that returns a reference

牧云@^-^@ 提交于 2019-11-30 11:21:13
Considering the following code: fn foo<'a, T: 'a>(t: T) -> Box<Fn() -> &'a T + 'a> { Box::new(move || &t) } What I expect: The type T has lifetime 'a . The value t live as long as T . t moves to the closure, so the closure live as long as t The closure returns a reference to t which was moved to the closure. So the reference is valid as long as the closure exists. There is no lifetime problem, the code compiles. What actually happens: The code does not compile: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements --> src/lib.rs:2:22 | 2 | Box

How do I efficiently build a vector and an index of that vector while processing a data stream?

柔情痞子 提交于 2019-11-29 19:35:06
问题 I have a struct Foo : struct Foo { v: String, // Other data not important for the question } I want to handle a data stream and save the result into Vec<Foo> and also create an index for this Vec<Foo> on the field Foo::v . I want to use a HashMap<&str, usize> for the index, where the keys will be &Foo::v and the value is the position in the Vec<Foo> , but I'm open to other suggestions. I want to do the data stream handling as fast as possible, which requires not doing obvious things twice.

Cannot infer an appropriate lifetime for a closure that returns a reference

半世苍凉 提交于 2019-11-29 16:59:28
问题 Considering the following code: fn foo<'a, T: 'a>(t: T) -> Box<Fn() -> &'a T + 'a> { Box::new(move || &t) } What I expect: The type T has lifetime 'a . The value t live as long as T . t moves to the closure, so the closure live as long as t The closure returns a reference to t which was moved to the closure. So the reference is valid as long as the closure exists. There is no lifetime problem, the code compiles. What actually happens: The code does not compile: error[E0495]: cannot infer an

How should I restructure my graph code to avoid an “Cannot borrow variable as mutable more than once at a time” error?

醉酒当歌 提交于 2019-11-28 09:55:40
问题 I have a simple graph that successfully compiles: use std::collections::HashMap; type Key = usize; type Weight = usize; #[derive(Debug)] pub struct Node<T> { key: Key, value: T, } impl<T> Node<T> { fn new(key: Key, value: T) -> Self { Node { key: key, value: value, } } } #[derive(Debug)] pub struct Graph<T> { map: HashMap<Key, HashMap<Key, Weight>>, list: HashMap<Key, Node<T>>, next_key: Key, } impl<T> Graph<T> { pub fn new() -> Self { Graph { map: HashMap::new(), list: HashMap::new(), next