Skip to main content
How are we doing? Please help us improve Stack Overflow. Take our short survey
1 vote
2 answers
46 views

Comparing Swift's and Rust's function signatures

I want to check my understanding of how Swift and Rust handle function parameters. From what I’ve seen, Swift has two main parameter modes: Pass by value, immutable: f(x: T) and Pass by reference, ...
Xander's user avatar
  • 11
4 votes
1 answer
57 views

If the return value of a function has the same lifetime as one of the arguments, then the return value is considered a borrow of the argument?

Where in Rust's specification does it say that if the returned reference of a function has the same lifetime as one of the reference arguments, then the returned reference is considered a borrow of ...
palapapa's user avatar
  • 1,041
0 votes
0 answers
73 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 ...
Stefan's user avatar
  • 28.7k
1 vote
0 answers
72 views

Why does the rust compiler keep a mutable reference here? [duplicate]

Consider the following program: struct RNG { numbers: Vec<u32>, } impl RNG { pub fn new() -> RNG { RNG { numbers: vec![] } } pub fn generate_random_numbers(&mut ...
mchl12's user avatar
  • 351
1 vote
2 answers
79 views

Question about Rust bracket operator plus ampersand

The following snippet is from the (Brown University ver.) Rust book fn largest<T>(list: &[T]) -> &T { let mut largest = &list[0]; for item in list { if item > ...
Jason Yao's user avatar
2 votes
2 answers
63 views

Is there a way to release and recapture references in closures while iterating?

I have code analogous to: struct L { length: usize, count: usize, } impl L { fn iter(&self, ns: impl Iterator<Item=usize>) -> impl Iterator<Item=usize> { ns....
aleferna's user avatar
  • 141
-2 votes
2 answers
109 views

Converting Option<String> to Option<&str> in match statement

I have a function that takes in a Option<&str>, but my var is currently a Result<String> that I have converted to Option<String> in a match statement. let geom_source= std::fs::...
user30757960's user avatar
1 vote
1 answer
133 views

Why does this conditional assignment give a borrow error when inside a loop but not outside?

This code // No particular meaning, just MVCE extracted from larger program pub fn foo(mut v: Vec<i32>) { let x = &v[0]; for _ in [0, 1] { if *x == 0 { v[0] = 0; ...
yugr's user avatar
  • 22.5k
1 vote
0 answers
99 views

Why does Miri report UB in this safe looking manual String::from_raw_parts usage after Vec::set_len(0)?

I'm experimenting with manually handling the deallocation of Strings stored in a Vec<String> by taking raw parts and calling String::from_raw_parts after setting the vec's length to 0. The ...
Plz help's user avatar
  • 131
2 votes
4 answers
231 views

How to define Rust HashMap where the keys are refences to the values?

I am porting some of my more complex C++ code over to Rust as a way to learn the language. One thing I have is a map of values keyed by a std::string held inside the value type, to avoid copying the ...
user avatar
3 votes
3 answers
166 views

Given a pointer x, why does setting y = x pass ownership, but y = &*x does not?

I am trying to understand the concept of ownership better. The code below does not compile: fn main() { let x = Box::new(1); let y = x; println!("{}", x); } This makes sense, because ...
ainarain's user avatar
0 votes
0 answers
39 views

Shared logic to return both a mutable and non-mutable reference in Rust [duplicate]

I'm fairly new to Rust, and this is a pattern that I've observed a few times. In the contrived example below, I have a struct with two fields of the same type: first: Vec<u32> and second: Vec<...
jjoelson's user avatar
  • 6,021
3 votes
2 answers
89 views

Insert to a HashMap if the key doesn't already exist, without cloning the key if it already exists?

I have a HashMap whose key type holds a heap allocation. I don't know if the HashMap already contains a particular key, and I would like to insert a new entry if the key doesn't already exist. How ...
Bernard's user avatar
  • 5,770
-1 votes
1 answer
60 views

How do I move a Sender object out of a mutable reference to a Vector of tuples of Strings and Sender Objects?

This is not a solution to my problem. The suggested answer replaces the values with Nones. I quite literally require the size of my vector to reduce. MRE: use tokio::sync::mpsc; #[tokio::main] async ...
kesarling's user avatar
  • 2,310
0 votes
1 answer
60 views

modify function to also return a mutable reference to a field of &mut self

Here is a rust function from my project llms-client::gemini::types::Session::update() pub(super) fn update(&mut self, reply: &str) { let history = &mut self.history; if ...
Suryansh Dey's user avatar

15 30 50 per page
1
2 3 4 5
105