borrowing

Why does modifying a mutable reference's value through a raw pointer not violate Rust's aliasing rules?

自古美人都是妖i 提交于 2019-12-05 23:23:04
问题 I don't have a particularly solid understanding of Rust's aliasing rules (and from what I've heard they're not solidly defined), but I'm having trouble understanding what makes this code example in the std::slice documentation okay. I'll repeat it here: let x = &mut [1, 2, 4]; let x_ptr = x.as_mut_ptr(); unsafe { for i in 0..x.len() { *x_ptr.offset(i as isize) += 2; } } assert_eq!(x, &[3, 4, 6]); The problem I see here is that x , being an &mut reference, can be assumed to be unique by the

Pass self reference to contained object's function

我只是一个虾纸丫 提交于 2019-12-05 18:48:12
I'm trying to grok Rust's ownership model. I'm trying to pass a reference to a containing object when calling a function on a struct. Here's my struct: pub struct Player {} impl Player { pub fn receive(self, app: &App) { } } As you can see, receive expects a reference to an App object. pub struct App { pub player: Player, } impl App { pub fn sender(self) { // how to call player.test() and pass self as a reference? self.player.receive(&self); } } The above code gives me "use of partially moved value: self ". Which makes sense, because App has move semantics so the value was moved into the

Why does modifying a mutable reference's value through a raw pointer not violate Rust's aliasing rules?

巧了我就是萌 提交于 2019-12-04 03:18:31
I don't have a particularly solid understanding of Rust's aliasing rules (and from what I've heard they're not solidly defined), but I'm having trouble understanding what makes this code example in the std::slice documentation okay. I'll repeat it here: let x = &mut [1, 2, 4]; let x_ptr = x.as_mut_ptr(); unsafe { for i in 0..x.len() { *x_ptr.offset(i as isize) += 2; } } assert_eq!(x, &[3, 4, 6]); The problem I see here is that x , being an &mut reference, can be assumed to be unique by the compiler. The contents of x get modified through x_ptr , and then read back via x , and I see no reason

How do you replace the value of a mutable variable by taking ownership of it?

前提是你 提交于 2019-12-02 12:13:25
问题 I am working with a LinkedList and I want to remove all elements which do not pass a test. However, I am running into the error cannot move out of borrowed content . From what I understand, this is because I am working with &mut self , so I do not have the right to invalidate (i.e. move) one of the contained values even for a moment to construct a new list of its values. In C++/Java, I would simply iterate the list and remove any elements which match a criteria. As there is no remove that I

What are the semantics of mutably borrowing a literal in Rust? [duplicate]

雨燕双飞 提交于 2019-12-02 10:34:58
问题 This question already has answers here : Why is it legal to borrow a temporary? (3 answers) Why isn't this rvalue promoted to an lvalue as specified in the reference? (1 answer) Closed last year . I found that this is able to compile: let x = &mut 10; *x = 20; This is very confusing. What are the semantics of mutably borrowing an literal? I come from C++, where the compiler will definitely not allow me to refer to a rvalue like this: int *x = &10; int &y = 10; 回答1: Like C++, Rust has the

How to make mutable pointer to field of node of tree and mutate it? [duplicate]

一笑奈何 提交于 2019-12-02 07:13:29
This question already has an answer here: Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time 4 answers I want to find some node in the tree and I need a pointer to the container of nodes: &mut Vec<Node> struct Node { c: Vec<Node>, v: i32, } impl Node { pub fn new(u: i32, n: Node) -> Node { let mut no = Node { c: Vec::new(), v: u, }; no.c.push(n); no } } fn main() { let mut a = Node::new(1, Node::new(2, Node::new(3, Node::new(4, Node::new(5, Node { c: Vec::new(), v: 6, }))))); let mut p: &mut Vec<Node> = &mut a.c; while p

What are the semantics of mutably borrowing a literal in Rust? [duplicate]

喜你入骨 提交于 2019-12-02 06:34:21
This question already has an answer here: Why is it legal to borrow a temporary? 3 answers Why isn't this rvalue promoted to an lvalue as specified in the reference? 1 answer I found that this is able to compile: let x = &mut 10; *x = 20; This is very confusing. What are the semantics of mutably borrowing an literal? I come from C++, where the compiler will definitely not allow me to refer to a rvalue like this: int *x = &10; int &y = 10; Like C++, Rust has the concept of rvalues and lvalue. The reference calls them value expressions (rvalue) and place expressions (lvalue). Additionally, there

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

别说谁变了你拦得住时间么 提交于 2019-12-02 06:25:01
问题 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

How do you replace the value of a mutable variable by taking ownership of it?

[亡魂溺海] 提交于 2019-12-02 05:54:32
I am working with a LinkedList and I want to remove all elements which do not pass a test. However, I am running into the error cannot move out of borrowed content . From what I understand, this is because I am working with &mut self , so I do not have the right to invalidate (i.e. move) one of the contained values even for a moment to construct a new list of its values. In C++/Java, I would simply iterate the list and remove any elements which match a criteria. As there is no remove that I have yet found, I have interpreted it as an iterate, filter, and collect. The goal is to avoid creating

Why does cloning data inside a closure not prevent the error “closure may outlive the current function”?

一曲冷凌霜 提交于 2019-12-02 02:05:51
I built a GTK application with gtk-rs. When I build the main window, I want to use some dynamic parameters such as window height. I created a struct which contains all such settings and want to use this as an input parameter for the function building the UI: fn main() { let application = gtk::Application::new(Some("id"), Default::default()) .expect("Initialization failed..."); let config = Config {width: 100., height: 100.}; application.connect_activate(|app| { build_ui(app, config.clone()); }); // Use config further application.run(&args().collect::<Vec<_>>()); } #[derive(Debug, Clone)] pub