ownership

What is the difference between a __weak and a __block reference?

天涯浪子 提交于 2019-11-27 06:15:48
I'm reading Xcode's documentation, and here is something that puzzles me: __block typeof(self) tmpSelf = self; [self methodThatTakesABlock:^ { [tmpSelf doSomething]; }]; The following is copied from the documentation: A block forms a strong reference to variables it captures. If you use self within a block, the block forms a strong reference to self , so if self also has a strong reference to the block (which it typically does), a strong reference cycle results. To avoid the cycle, you need to create a weak (or __block ) reference to self outside the block, as in the example above. I don't

Type mismatches resolving a closure that takes arguments by reference

被刻印的时光 ゝ 提交于 2019-11-27 04:52:09
问题 I'm encountering a strange pair of errors while trying to compile my Rust code below. In searching for others with similar problems, I came across another question with the same combination of (seemingly opposing) errors, but couldn't generalize the solution from there to my problem. Basically, I seem to be missing a subtlety in Rust's ownership system. In trying to compile the (very pared down) code here: struct Point { x: f32, y: f32, } fn fold<S, T, F>(item: &[S], accum: T, f: F) -> T

Convert Vec<String> into a slice of &str in Rust?

徘徊边缘 提交于 2019-11-27 02:34:46
问题 Per Steve Klabnik's writeup in the pre-Rust 1.0 documentation on the difference between String and &str, in Rust you should use &str unless you really need to have ownership over a String . Similarly, it's recommended to use references to slices ( &[] ) instead of Vec s unless you really need ownership over the Vec . I have a Vec<String> and I want to write a function that uses this sequence of strings and it doesn't need ownership over the Vec or String instances, should that function take &

What are move semantics in Rust?

谁都会走 提交于 2019-11-27 01:08:12
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 of borrows, but not both at the same time: one or more references ( &T ) to a resource, exactly one

Taking ownership of files with 'broken' permissions

Deadly 提交于 2019-11-26 23:09:20
问题 I'm trying to overcome the following situation. Given a directory stored on an NTFS volume, where: The directory owner is set to someone else (a non-privileged user for example) The directory DACL is configured to permit access to a specific group of people that does not include the system or Administrators The DACL on the directory actually grants no one access to either take ownership or change the DACL (or in short, the all administrators have been locked out of the folder) But! The

Implement graph-like datastructure in Rust

一笑奈何 提交于 2019-11-26 20:57:04
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 be mutable because I need to be able to add and remove links and resources at runtime. Because of this,

Temporarily move out of borrowed content

醉酒当歌 提交于 2019-11-26 19:12:05
I'm tring to replace a value in a mutable borrow; moving part of it into the new value: enum Foo<T> { Bar(T), Baz(T), } impl<T> Foo<T> { fn switch(&mut self) { *self = match self { &mut Foo::Bar(val) => Foo::Baz(val), &mut Foo::Baz(val) => Foo::Bar(val), } } } The code above doesn't work, and understandibly so, moving the value out of self breaks the integrity of it. But since that value is dropped immediately afterwards, I (if not the compiler) could guarantee it's safety. Is there some way to achieve this? I feel like this is a job for unsafe code, but I'm not sure how that would work.

Does println! borrow or own the variable?

回眸只為那壹抹淺笑 提交于 2019-11-26 15:27:30
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 correct and is the same result as the first code. Chris Morgan The macros print! , println! , eprint! ,

Is it possible to return either a borrowed or owned type in Rust?

喜夏-厌秋 提交于 2019-11-26 11:37:48
问题 In the following code, how can I return the reference of floor instead of a new object? Is it possible to let the function return either a borrowed reference or an owned value? extern crate num; // 0.2.0 use num::bigint::BigInt; fn cal(a: BigInt, b: BigInt, floor: &BigInt) -> BigInt { let c: BigInt = a - b; if c.ge(floor) { c } else { floor.clone() } } 回答1: Since BigInt implements Clone , you can use a std::borrow::Cow: use num::bigint::BigInt; // 0.2.0 use std::borrow::Cow; fn cal(a: BigInt,

Is there an owned version of String::chars?

♀尐吖头ヾ 提交于 2019-11-26 10:02:12
问题 The following code does not compile: use std::str::Chars; struct Chunks { remaining: Chars, } impl Chunks { fn new(s: String) -> Self { Chunks { remaining: s.chars(), } } } The error is: error[E0106]: missing lifetime specifier --> src/main.rs:4:16 | 4 | remaining: Chars, | ^^^^^ expected lifetime parameter Chars doesn\'t own the characters it iterates over and it can\'t outlive the &str or String it was created from. Is there an owned version of Chars that does not need a lifetime parameter