805 questions
3
votes
1
answer
98
views
What options exist for ownership/references in Rust dependency injection?
I'm interested in the options for providing ownership or references to dependencies when doing dependency injection in Rust. I don't mean like a DI framework or anything like that, just the general ...
0
votes
0
answers
74
views
Non-bitwise move or `Move` trait? [duplicate]
In Rust, how can I define&manipulate objects that cannot be just copied bit-by-bit when they're moved?
For example, an object that contains a relative pointer (i.e. a pointer whose target is ...
8
votes
2
answers
285
views
Is a shared reference in Rust cheaper than transferring ownership?
As far as I understand transferring ownership in Rust is a more expensive operation than transferring a value by shared or exclusive reference.
As far as I understand from What is ownership? from the ...
0
votes
0
answers
42
views
Cannot borrow `*self` as mutable more than once at a time [duplicate]
The Error
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src\scanner\mod.rs:103:17
|
45 | impl<'a> Scanner<'a> {
| -- lifetime `'a` defined ...
0
votes
1
answer
75
views
Rust struct member references another member [duplicate]
I was trying to implement a system in Rust that has one struct that owns a value and another struct, which needs a reference to that value when created. It seems this is not really possible in any ...
0
votes
1
answer
152
views
How to avoid explicitly cloning a variable that is moved to a closure?
Is there a way to avoid creating temporary variables a variable is captured in multiple closures:
The move keyword in the closures is required
It's fine to use any other container instead of Rc as ...
-1
votes
1
answer
85
views
Move occur because tree has type which does not implement the copy trait, value moved here
I know it's a redundant question with rust but it seems I can not manage to get some generality from it. So I attempted to write a simple binary tree using some code from elsewhere (at first I used a ...
0
votes
0
answers
35
views
How to store an array and a slice of that array in a struct in Rust? [duplicate]
I have a struct to save data read from a socket connection. This struct has an array field buffer which provides space for the read data to be saved in. Now, I need to way to save which part of that ...
1
vote
2
answers
111
views
c++ passing ownership to composite class
I have an object (let's call it A) that I want to store another object (let's say it has type B and is called m), which will be passed from the function that creates A. The creation of B is made by ...
0
votes
0
answers
274
views
Return largest shareholder ownership - Bloomberg
I am attempting to return the ownership percentage of the largest owner from several stocks using Bloomberg API in excel. Does anyone know of a relevant function to use?
I have found "...
-1
votes
1
answer
45
views
Rust can't figure out the type of a borrowed value [duplicate]
type NodePointer<T> = Option<Rc<RefCell<Node<T>>>>;
#[derive(Debug)]
struct Node<T> {
val: T,
next: NodePointer<T>
}
pub struct LinkedList<T> {...
0
votes
0
answers
41
views
Move ownership of uncopiable type [duplicate]
I'm a relatively new Rust dev and this is an issue I've been running into consistently. Sometimes I have data stored within a Vector which I will later move into a new variable, but if the data isn't ...
0
votes
1
answer
167
views
Lifetime of items in impl Iterator of Reqwest Cookies
I just started writing in Rust and I am writing a small program to learn Rust and the package Reqwest.
Here's my little program. I don't understand why changing from HashMap<&str, &str> ...
1
vote
1
answer
145
views
How to avoid a clone when passing a value to a consuming function, but needing it back in the event of an error?
Say I have a function that calls a Rust API that consumes a value. That API function returns a Result, potentially indicating an error. If the error happens, I need some information from the original ...
0
votes
0
answers
45
views
Rust Compiller maybe bugged [duplicate]
An error occurs at a very simple place. When a reference enters a for loop, there is an implicit conversion to into_iter() on a mutable reference, and as we know, this also implicitly uses the ...
1
vote
2
answers
124
views
Error: Use of moved value, while it cannot be used
Consider the following code:
fn print_or(opt: Option<()>, tail: Vec<i32>) -> Vec<i32> {
opt.map_or(tail, |_| {
println!("{:?}", tail);
tail
})
}
...
0
votes
1
answer
86
views
Rust Lifetime Error: Cannot Return Value Referencing Local Variable
I am working on a Rust project using rusb to interact with USB devices. I'm facing a lifetime management issue where I cannot return a struct containing a reference due to it referencing a local ...
0
votes
1
answer
168
views
Represent a consumable, single-use object in TypeScript
In Rust, a struct can have a method that takes ownership of the struct and moves it, making it illegal to use the struct again later, e.g.
struct Foo {}
impl Foo {
fn consume(self) -> u32 {
...
0
votes
0
answers
59
views
Iterator that returns windows into an owned buffer – without copying? [duplicate]
The Issue
I'm trying to implement an iterator that yields windows (i.e. slices) into a buffer owned by the iterator itself – for example, if the buffer contains [0, 1, 2, 3, 4, 5], the iterator might ...
1
vote
2
answers
123
views
Returning the raw pointer from a list of unique pointers
I have a class (A) that will own the memory for a set of instances of another class (B). A will store its instances of B as a set of std::unique_ptr<B>.
I want A to be able to return a reference ...
2
votes
1
answer
138
views
Return ownership from closure after thread finised in Rust
I am learning Rust and am now taking a look at threads. I am trying to convert the following snippet into a parallelized execution:
fn main() {
let mut data: Vec<usize> = (1..22).collect();
...
-1
votes
1
answer
67
views
Cannot understand why this code is working according to the rules of ownership
I have the following code
use euclid::Vector3D;
fn main() {
fn add_vectors<Unit>(a: Vector3D::<f64, Unit>, b: Vector3D::<f64, Unit>) -> Vector3D::<f64, Unit> {
/...
0
votes
1
answer
55
views
Borrowing issues using variables wrapped in Arc<Mutex<>> in Rust [duplicate]
The code below can work.
#[derive(Debug)]
struct Test{
str1 : Option<String>,
str2 : Option<String>,
}
fn main() {
let mut test = Test{
str1 : Some(String::from("...
0
votes
2
answers
173
views
How do I create a Rust struct that can link to another instance of itself?
I'm trying to create a chain of struct instances, like this:
Struct_instance_a:
id:1
prev:None
Struct_instance_b:
id:2
prev:Struct_instance_a
etc..
But getting this error from the compiler:
error[...
0
votes
0
answers
61
views
Extracting Option<String> fields with yaml_rust
New rust user here.
I'm trying to use yaml_rust to parse my config files, and spotted a difficulty.
I am trying to keep Option<...> values to handle the possibility that a field is missing or ...