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
21
votes
2 answers

How to keep git from changing file ownership

I have been noticing that when I pull from my github repo on a development server(Red Hat) the ownership of the files change after the pull is completed. The .git file used to be owned by me but then I noticed that it would write files as me and I…
user2108258
  • 803
  • 3
  • 13
  • 20
21
votes
5 answers

single vs shared ownership meaning

Was reading Wikipedia for RAII when just saw Single and Shared ownership. Googled for it and couldn't find any useful answer! Could some one possibly explain this concept for a schoolboy?
Amir
  • 9,577
  • 11
  • 41
  • 58
20
votes
7 answers

What is the right way to expose resources owned by a class?

Let's say I have a library which has a Document class. An instance of Document can own several instances of Field. Field has multiple subclasses (for example IntegerField and StringField), and even the API user can subclass it and supply subclass…
Venemo
  • 18,515
  • 13
  • 84
  • 125
19
votes
3 answers

How to fix ".. was mutably borrowed here in the previous iteration of the loop" in Rust?

I have to iterate on keys, find the value in HashMap by key, possibly do some heavy computation in the found struct as a value (lazy => mutate the struct) and cached return it in Rust. I'm getting the following error message: error[E0499]: cannot…
4ntoine
  • 19,816
  • 21
  • 96
  • 220
19
votes
2 answers

Understanding usage of Rc> in Rust

I'm looking at some code that uses Rc> So I went out to read about the differences between Rc and RefCell: Here is a recap of the reasons to choose Box, Rc, or RefCell: Rc enables multiple owners of the same data; Box and…
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
16
votes
2 answers

Idiomatic Clojure way to spawn and manage background threads

What is the idiomatic Clojure way to create a thread that loops in the background doing updates to some shared refs and to manage its lifetime? I find myself using future for this, but it feels like a little bit of a hack as I never return a…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
16
votes
2 answers

How do I copy a String from a &String while iterating through a Vector?

I'm trying to get my head around ownership and the borrow checker. I've run into an issue I've managed to 'solve' but I think there should be a more ergonomic way to do it. The following code trips the borrow checker, because I'm trying to move…
arsalan86
  • 575
  • 2
  • 5
  • 9
14
votes
1 answer

Why does `set` method defined on `Cell` explicitly drops the old value? (Rust)

Interested why does set method defined on Cell, on the last line explicitly drops old value. Shouldn't it be implicitly dropped (memory freed) anyways when the function returns? use std::mem; use std::cell::UnsafeCell; pub struct Cell { …
Newb
  • 181
  • 1
  • 7
14
votes
2 answers

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?

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…
14
votes
1 answer

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

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,…
lucian
  • 527
  • 5
  • 15
13
votes
2 answers

How to decide when function input params should be references or not?

When writing a function how does one decide whether to make input parameters referenced or consumed? For example, should I do this? fn foo(val: Bar) -> bool { check(val) } // version 1 Or use referenced param instead? fn foo(val: &Bar) -> bool {…
prior
  • 339
  • 4
  • 10
13
votes
1 answer

In Rust, can you own a string literal?

According to The Rust book: Each value in Rust has a variable that’s called its owner. There can be only one owner at a time. When the owner goes out of scope, the value will be dropped. According to rust-lang.org: Static items do not call drop…
mellow-yellow
  • 1,670
  • 1
  • 18
  • 38
13
votes
3 answers

Temporarily move out of borrowed content

I'm tring to replace a value in a mutable borrow; moving part of it into the new value: enum Foo { Bar(T), Baz(T), } impl Foo { fn switch(&mut self) { *self = match self { &mut Foo::Bar(val) =>…
azgult
  • 542
  • 3
  • 10
13
votes
3 answers

Suicide object implementation leveraging `std::weak_ptr`

I'm considering using "suicide objects" to model entities in a game, that is, objects able to delete themselves. Now, the usual C++03 implementation (plain old delete this) does nothing for other objects potentially refering to the suicide object,…
Quentin
  • 62,093
  • 7
  • 131
  • 191
13
votes
5 answers

c++11 - Ownership and getters

I'm new to C++ and I have troubles wrapping my head around ownership, specifically with a getter. Here's some example code: class GameObject { public: Transform *transform(); private: Transform _transform; }; I guess a raw pointer is unsafe to…
cboe
  • 469
  • 1
  • 9
  • 25
1
2
3
49 50