ownership

Does non-matching arm take the owner of a variable in a “match” statement in Rust?

ぃ、小莉子 提交于 2021-02-16 21:32:06
问题 I'm new to Rust. Below is my testing. #[derive(Debug)] enum Food { Cake, Pizza, Salad, } #[derive(Debug)] struct Bag { food: Food } fn main() { let bag = Bag { food: Food::Cake }; match bag.food { Food::Cake => println!("I got cake"), x => println!("I got {:?}", x) } println!("{:?}", bag); } When I run it, I got an error. error[E0382]: borrow of moved value: `bag` --> src\main.rs:20:22 | 17 | x => println!("I got {:?}", x) | - value moved here ... 20 | println!("{:?}", bag); | ^^^ value

Does non-matching arm take the owner of a variable in a “match” statement in Rust?

耗尽温柔 提交于 2021-02-16 21:30:05
问题 I'm new to Rust. Below is my testing. #[derive(Debug)] enum Food { Cake, Pizza, Salad, } #[derive(Debug)] struct Bag { food: Food } fn main() { let bag = Bag { food: Food::Cake }; match bag.food { Food::Cake => println!("I got cake"), x => println!("I got {:?}", x) } println!("{:?}", bag); } When I run it, I got an error. error[E0382]: borrow of moved value: `bag` --> src\main.rs:20:22 | 17 | x => println!("I got {:?}", x) | - value moved here ... 20 | println!("{:?}", bag); | ^^^ value

Better shared_ptr by distinct types for “ownership” and “reference”?

*爱你&永不变心* 提交于 2021-02-05 10:44:05
问题 I would like to use a pointer-like object Ownership<Type> m_foo for the owning object and handle Reference<Type> m_someFoo as a classical "pointer" in another context, whereas my Reference should know when the original object does not exist anymore (e.g. by returning nullptr) and it should furthermore be possible to prevent the original object from deletion for a small period of time (locking). I know that shared_ptr (Ownership) and weak_ptr (Reference) provide similar functionality. However,

The proper ownership for “caching proxy” in Rust?

瘦欲@ 提交于 2021-01-29 20:00:58
问题 I'd like to use Factory to build an object from the String and have multiple impls: 1) actual building and 2) caching (stores in-memory in HashMap ). The problem is that in case #1 it have to pass the ownership and in case #2 HashMap owns the value and a reference can be returned only. use std::collections::HashMap; // product interface pub trait TProduct { fn get_title(&self) -> &String; } // and concrete impls pub struct ConcreteProduct1 { } impl TProduct for ConcreteProduct1 { // ... } pub

The proper ownership for “caching proxy” in Rust?

不羁的心 提交于 2021-01-29 15:48:25
问题 I'd like to use Factory to build an object from the String and have multiple impls: 1) actual building and 2) caching (stores in-memory in HashMap ). The problem is that in case #1 it have to pass the ownership and in case #2 HashMap owns the value and a reference can be returned only. use std::collections::HashMap; // product interface pub trait TProduct { fn get_title(&self) -> &String; } // and concrete impls pub struct ConcreteProduct1 { } impl TProduct for ConcreteProduct1 { // ... } pub

How to force EclipseLink's @PrivateOwned to perform delete before insert

懵懂的女人 提交于 2020-08-02 12:29:25
问题 I have an entity which has @OneToMany private ownership of a collection of another entities. That other entity has some unique constraints over different set of columns. The problem arises when I want to update the main entity (together with changed child entities). EclipseLink does insert before delete, so sometimes, an insertion violates the constraint and throws an exception. Is there a way to force the deletion of child entities before inserting their updated counterparts? 回答1: I know, I

How to force EclipseLink's @PrivateOwned to perform delete before insert

China☆狼群 提交于 2020-08-02 12:27:04
问题 I have an entity which has @OneToMany private ownership of a collection of another entities. That other entity has some unique constraints over different set of columns. The problem arises when I want to update the main entity (together with changed child entities). EclipseLink does insert before delete, so sometimes, an insertion violates the constraint and throws an exception. Is there a way to force the deletion of child entities before inserting their updated counterparts? 回答1: I know, I

How to run for loop on elements of a vector and change the vector inside the for loop and outside the for loop in rust?

耗尽温柔 提交于 2020-06-26 06:02:38
问题 I am new to Rust . I need to create a vector before a for loop. Run for loop on it. Change the vector inside the for loop. Then Change the vector after the for loop. I tried the following code and tried to use immutable borrow but both did not work. fn main() { let mut vec1 = vec![4, 5]; vec1.push(6); for i in vec1 { if i % 2 == 0 { vec1.push(7); } } vec1.push(8); println!("vec1={:?}", vec1); } I expect to compile and change the vector inside and after the for loop. But it shows this error