ownership

How does Rust know which types own resources?

£可爱£侵袭症+ 提交于 2019-12-10 15:25:57
问题 When one has a box pointer to some heap-allocated memory, I assume that Rust has 'hardcoded' knowledge of ownership, so that when ownership is transferred by calling some function, the resources are moved and the argument in the function is the new owner. However, how does this happen for vectors for example? They too 'own' their resources, and ownership mechanics apply like for box pointers -- yet they are regular values stored in variables themselves , and not pointers. How does Rust (know

Objective C “autorelease” in C++ — standard way to control object lifetime?

半世苍凉 提交于 2019-12-10 14:56:03
问题 I'm in the process of porting some code from Objective C to C++. I'm not as familiar with C++ design patterns as I am with Objective C. In the Cocoa world, there is the very common pattern of writing a factory method that returns an "autoreleased" object. Somethings as simple as: - (MyClass *)load { MyClass* obj = [[MyClass alloc] init]; return [obj autorelease]; } This is simple and easy to understand. The method owns the memory it allocates, but can hand it back to the caller, while

Ownership and conditionally executed code

别说谁变了你拦得住时间么 提交于 2019-12-08 20:33:36
问题 I read the rust book over the weekend and I have a question about the concept of ownership. The impression I got is that ownership is used to statically determine where a resource can be deallocated. Now, suppose that we have the following: { // 1 let x; // 2 { // 3 let y = Box::new(1); // 4 x = if flip_coin() {y} else {Box::new(2)} // 5 } // 6 } // 7 I was surprised to see that the compiler accepts this program. By inserting println! s and implementing the Drop trait for the boxed value, I

Use unique_ptr for ownership and raw pointer otherwise?

我只是一个虾纸丫 提交于 2019-12-08 16:43:45
问题 I am C++11-ing some code. I have class X { /* */ }; class A { std::vector<X*> va_x; }; class B { std::vector<X*> vb_x; std::vector<A> vb_a; }; The X*s of "va_x" inside my class A point to objects that are also pointed to by the X*s of "vb_x" inside my class B. Now I would like to use smart pointers. For me, it seems clear that class B has the ownership of the objects pointed by the X* (in particular because my A instances belong to B) So I should use a unique_ptr for X inside B: class B { std

How is my reference to a struct member still valid after the struct was moved?

孤者浪人 提交于 2019-12-07 23:03:46
问题 I'm creating a reference to a structure member using a function (named get ), then I move the struct using another function (named pr ), then I dereference the previously created pointer. Am I in the wrong here (a.k.a. working by accident), or is my reference is still valid by some rule? struct MyStruct { inner: i32, } fn get(a: &MyStruct) -> &i32 { return &a.inner; } fn pr(a: MyStruct) { println!("MyStruct {}", a.inner); } fn main() { println!("Hello, world!"); let x = MyStruct { inner: 3 };

How to transfer ownership of a value to C code from Rust?

爱⌒轻易说出口 提交于 2019-12-07 04:01:07
问题 I'm trying to write some Rust code with FFI that involves C taking ownership of a struct: fn some_function() { let c = SomeStruct::new(); unsafe { c_function(&mut c); } } I want c_function to take ownership of c . In C++, this could be achieved by the release method of unqiue_ptr . Is there something similar in Rust? 回答1: The std::unique_ptr type in C++ corresponds to Box in Rust, and .release() corresponds to Box::into_raw. let c = Box::new(SomeStruct::new()); unsafe { c_function(Box::into

Why does a function that accepts a Box<MyType> complain of a value being moved when a function that accepts self works?

谁说胖子不能爱 提交于 2019-12-06 07:09:52
I have the following code which compiles: pub mod Btree { pub struct node { pub id: u32, pub red: bool, pub left: Option<Box<node>>, pub right: Option<Box<node>>, } impl<'a> node { pub fn insert(mut node_: Option<Box<node>>, id: u32) -> Option<Box<node>> { match node_ { None => Some(Box::new(node { id: id, red: true, left: None, right: None, })), Some(x) => x.insert_(id), } } pub fn insert_(mut self, id: u32) -> Option<Box<node>> { self.left = node::insert(self.left, id); Some(Box::new(self)) } } } When I change insert_() to work with a Box<node> instead: pub mod Btree { pub struct node { pub

When would an implementation want to take ownership of self in Rust?

狂风中的少年 提交于 2019-12-06 01:54:27
问题 I'm reading through the Rust documentation on lifetimes. I tried something like: struct S { x: i8, } impl S { fn fun(self) {} fn print(&self) { println!("{}", self.x); } } fn main() { let s = S { x: 1 }; s.fun(); s.print(); } I get the following error: error[E0382]: borrow of moved value: `s` --> src/main.rs:16:5 | 15 | s.fun(); | - value moved here 16 | s.print(); | ^ value borrowed here after move | = note: move occurs because `s` has type `S`, which does not implement the `Copy` trait This

How to transfer ownership of a value to C code from Rust?

ε祈祈猫儿з 提交于 2019-12-05 09:01:13
I'm trying to write some Rust code with FFI that involves C taking ownership of a struct: fn some_function() { let c = SomeStruct::new(); unsafe { c_function(&mut c); } } I want c_function to take ownership of c . In C++, this could be achieved by the release method of unqiue_ptr . Is there something similar in Rust? kennytm The std::unique_ptr type in C++ corresponds to Box in Rust, and .release() corresponds to Box::into_raw . let c = Box::new(SomeStruct::new()); unsafe { c_function(Box::into_raw(c)); } Note that the C function should return the ownership of the pointer to Rust to destroy

Operator overloading by value results in use of moved value

ぐ巨炮叔叔 提交于 2019-12-05 06:56:44
Compiling the following Rust code that uses operator overloading use std::ops::{Add}; #[derive(Show)] struct Point { x: int, y: int } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point {x: self.x + other.x, y: self.y + other.y} } } fn main() { let p: Point = Point {x: 1, y: 0}; let pp = p + p; } Results in compiler errors due to ownership of p: <anon>:21:18: 21:19 error: use of moved value: `p` <anon>:21 let pp = p + p; ^ <anon>:21:14: 21:15 note: `p` moved here because it has type `Point`, which is non-copyable <anon>:21 let pp = p + p; ^ The rationale