Questions tagged [ownership]

Ownership is a core concept of Rust. The system of ownership is a set of rules that the compiler checks at compile time to manage the memory. DO NOT USE FOR THE FILE OWNER USER AND GROUP IN UNIX SYSTEMS; for that, use [permissions] instead.

The three rules of ownership in are:

  1. Each value in Rust has a variable that’s called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.

You can read more in the documentation.

742 questions
90
votes
1 answer

Does println! borrow or own the variable?

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…
kevinyu
  • 1,367
  • 10
  • 12
81
votes
4 answers

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

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…
56
votes
2 answers

Convert Vec into a slice of &str in Rust?

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…
Don Rowe
  • 833
  • 1
  • 7
  • 17
47
votes
7 answers

how to find the owner of a file or directory in python

I need a function or method in Python to find the owner of a file or directory. The function should be like: >>> find_owner("/home/somedir/somefile") owner3
ramdaz
  • 1,761
  • 1
  • 20
  • 43
46
votes
1 answer

How to convert Option<&T> to Option in the most idiomatic way in Rust?

When using HashMap's get method, I get an Option<&T>, I've encountered it again this time with Option<&String>. I'd like to get an owned value Option. Is this possible without me writing map(|x| x.to_owned())? I'm just wondering if there's a…
opensourcegeek
  • 5,552
  • 7
  • 43
  • 64
45
votes
6 answers

What are move semantics in Rust?

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…
nalply
  • 26,770
  • 15
  • 78
  • 101
36
votes
1 answer

Do mutable references have move semantics?

fn main() { let mut name = String::from("Charlie"); let x = &mut name; let y = x; // x has been moved say_hello(y); say_hello(y); // but y has not been moved, it is still usable change_string(y); …
oberblastmeister
  • 864
  • 9
  • 13
34
votes
3 answers

Implement graph-like data structure in Rust

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, links:…
Lorenz
  • 2,179
  • 3
  • 19
  • 18
31
votes
1 answer

What is the right way to store an immutable Path in a struct?

The following code works but not sure if it is the right way. A few questions: Should I use Path or PathBuf? Should I use AsRef? Do I need PathBuf::from(path) in order to have path owned by the struct? use std::fmt; use std::path::PathBuf; struct…
Hernan
  • 5,811
  • 10
  • 51
  • 86
31
votes
1 answer

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

What is the proper way of transferring ownership of a std::vector > to a class being constructed? Below is a code representation of what I want to do. I realize it is not correct (won't compile) and violates "uniqueness" whether I…
Jen
  • 313
  • 1
  • 3
  • 6
27
votes
1 answer

What happens when assigning to the underscore pattern?

This is a question from Rust quiz 28: struct Guard; impl Drop for Guard { fn drop(&mut self) { print!("1"); } } fn main() { let _guard = Guard; print!("3"); let _ = Guard; print!("2"); } Such code prints 3121, in…
dpr
  • 319
  • 1
  • 6
26
votes
2 answers

Delphi Ownership Confusion

I always thought that the owner is responsible for destroying visual controls and that I can manually control destruction if I pass nil as the owner. Consider the following example: TMyForm = class (TForm) private FButton :…
jpfollenius
  • 16,456
  • 10
  • 90
  • 156
22
votes
3 answers

Why does Rust not allow the copy and drop traits on one type?

From the book: Rust won’t let us annotate a type with the Copy trait if the type, or any of its parts, has implemented the Drop trait. If the type needs something special to happen when the value goes out of scope and we add the Copy annotation to…
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
22
votes
4 answers

Cannot infer an appropriate lifetime for a closure that returns a reference

Considering the following code: fn foo<'a, T: 'a>(t: T) -> Box &'a T + 'a> { Box::new(move || &t) } What I expect: The type T has lifetime 'a. The value t live as long as T. t moves to the closure, so the closure live as long as t The…
xardas
  • 355
  • 1
  • 6
22
votes
1 answer

How can I take ownership of a Vec element and replace it with something else?

I am writing a function of the following format: fn pop(data: &mut Vec>) -> Option { // Let the item be the current element at head let item = data[0]; // and "remove" it. data[0] = None; item } When I try to…
Cameron Sun
  • 423
  • 1
  • 4
  • 9
1
2 3
49 50