ownership

What are move semantics in Rust?

白昼怎懂夜的黑 提交于 2019-11-26 07:37:13
问题 In Rust, there are two possibilities to take a reference Borrow , i.e., take a reference but don\'t allow mutating the reference destination. The & operator borrows ownership from a value. Borrow mutably , i.e., take a reference to mutate the destination. The &mut operator mutably borrows ownership from a value. The Rust documentation about borrowing rules says: First, any borrow must last for a scope no greater than that of the owner. Second, you may have one or the other of these two kinds

Implement graph-like datastructure in Rust

折月煮酒 提交于 2019-11-26 04:53:54
问题 I have a data structure which can be represented as a unidirectional graph between some structs linked with link objects because links contain metadata. It looks something like this: struct StateMachine { resources: Vec<Resource>, links: Vec<Link>, } struct Resource { kind: ResourceType, // ... } enum LinkTarget { ResourceList(Vec<&Resource>), LabelSelector(HashMap<String, String>), } struct Link { from: LinkTarget, to: LinkTarget, metadata: SomeMetadataStruct, } The whole structure needs to

Does println! borrow or own the variable?

耗尽温柔 提交于 2019-11-26 03:59:10
问题 I am confused with borrowing and ownership. In the Rust documentation about reference and borrowing let mut x = 5; { let y = &mut x; *y += 1; } println!(\"{}\", x); They say println! can borrow x . I am confused by this. If println! borrows x , why does it pass x not &x ? I try to run this code below fn main() { let mut x = 5; { let y = &mut x; *y += 1; } println!(\"{}\", &x); } This code is identical with the code above except I pass &x to println! . It prints \'6\' to the console which is