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
12
votes
1 answer

Should I take `self` by value or mutable reference when using the Builder pattern?

So far, I've seen two builder patterns in official Rust code and other crates: impl DataBuilder { pub fn new() -> DataBuilder { ... } pub fn arg1(&mut self, arg1: Arg1Type) -> &mut Builder { ... } pub fn arg2(&mut self, arg2: Arg2Type)…
Sprite
  • 3,222
  • 1
  • 12
  • 29
12
votes
1 answer

What does "borrowed data cannot be stored outside of its closure" mean?

When compiling the following code: fn main() { let mut fields = Vec::new(); let pusher = &mut |a: &str| { fields.push(a); }; } The compiler gives me the following error: error: borrowed data cannot be stored outside of its…
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
12
votes
1 answer

How to move a value out of an object-safe trait object?

A Mech carries a driver, which is a Named entity. At run-time, an omitted Mech constructor consults external source for the specific type of driver to use. trait Named { fn name(self) -> String; } struct Person { first_name: String, …
mwgkgk
  • 173
  • 8
12
votes
2 answers

When would an implementation want to take ownership of self in Rust?

I'm reading through the Rust documentation on lifetimes. I tried something like: struct S { x: i8, } impl S { fn fun(self) {} fn print(&self) { println!("{}", self.x); } } fn main() { let s = S { x: 1 }; s.fun(); …
Joe
  • 46,419
  • 33
  • 155
  • 245
12
votes
2 answers

the From<&String> trait is not implemented for the type String

I'm going off of this article in an attempt to write a function that accepts both a String and a &str, but I'm running into a problem. I have the following function: pub fn new(t_num: S) -> BigNum where S: Into { let t_value =…
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177
12
votes
2 answers

How to prevent root from running git pull?

Have need to prevent root from updating a git (working) directory. Reasoning includes but not limited to: preventing undersired file-system ownership changes. None of the git hooks seem to prevent a fetch/merge/pull before it happens, similar to…
Johnny Utahh
  • 2,389
  • 3
  • 25
  • 41
11
votes
2 answers

Why do I not need to explicitly lend a borrowed, mutable variable?

I've just written a small Rust program which calculates Fibonacci numbers and memoizes the calculation. It works, but I'm a little confused about why, especially the recursive call. (It also probably isn't idiomatic.) Here's the program: use…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
10
votes
1 answer

Entry::Occupied.get() returns a value referencing data owned by the current function even though hashmap should have the ownership

My goal was to implement the suggested improvement on the cacher struct of the rust book chapter 13.1, that is creating a struct which takes a function and uses memoization to reduce the number of calls of the given function. To do this, I created a…
Nico227
  • 326
  • 2
  • 9
10
votes
6 answers

Is there an owned version of String::chars?

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]:…
Challenger5
  • 959
  • 6
  • 26
10
votes
3 answers

Marking std::unique_ptr class member as const

A lot of the examples for using std::unique_ptr to manage ownership of class dependencies look like the following: class Parent { public: Parent(Child&& child) : _child(std::make_unique(std::move(child))){} private: …
Peet Whittaker
  • 750
  • 6
  • 31
9
votes
2 answers

Taking ownership of files with 'broken' permissions

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…
Kyle Brantley
  • 315
  • 1
  • 2
  • 9
9
votes
1 answer

How to transfer ownership of a value to C code from Rust?

I'm trying to write some Rust code with FFI that involves C taking ownership of a struct: fn some_function() { let c = SomeStruct::new(); unsafe { c_function(&mut c); } } I want c_function to take ownership of c. In C++, this…
kkspeed
  • 373
  • 2
  • 9
9
votes
2 answers

Operator overloading by value results in use of moved value

Compiling the following Rust code that uses operator overloading use std::ops::{Add}; #[derive(Show)] struct Point { x: int, y: int } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point…
zgerd
  • 1,080
  • 8
  • 17
8
votes
1 answer

Why can I not call FnMut twice in a line?

Taking example snippets from here: the following doesn't compile fn foobar(mut f: F) where F: FnMut(i32) -> i32 { println!("{}", f(f(2))); // error: cannot borrow `f` as mutable more than once at a time } fn main() { foobar(|x|…
nalzok
  • 14,965
  • 21
  • 72
  • 139
8
votes
2 answers

How to split a Vec into two without allocating memory?

I am looking for a function similar to slice::split_at_mut. Let's name it split_at with the signature pub fn split_at(v: Vec, mid: usize) -> (Vec, Vec) such that let v = vec![1, 2, 3, 4]; let (first, second) = split_at(v,…
nalzok
  • 14,965
  • 21
  • 72
  • 139
1 2
3
49 50