Skip to main content
Filter by
Sorted by
Tagged with
0 votes
2 answers
68 views

I am writing a scripting language in rust, and I'm pretty new to the language. When I compile source into byte code I move the compiled symbols (Chunk) out of the function. I am also lazy and don't ...
NongusStudios's user avatar
3 votes
0 answers
77 views

I am trying to develop a linear algebra library in rust. So, as part of it I am writing code that iterates through a n-dimensional array and returns mutable references. It's like writing a view for a ...
SpatialFuel's user avatar
4 votes
2 answers
123 views

This is a pattern I want to be able to use in my code to have global variables that contain non-static references. Is it memory safe? If not, how can I make it so? use std::cell::Cell; pub struct ...
Wesley Jones's user avatar
5 votes
1 answer
180 views

While trying to write an API inspired by the structure of std::thread::scope, I ran across the following problem. This function compiles fine (playground): use std::marker::PhantomData; pub struct ...
jacobsa's user avatar
  • 7,875
5 votes
1 answer
132 views

I am using the winnow crate to write a parser and am struggling with adding tests due to lifetime conflicts. The basic setup I have is a stateful stream: // The primary stream type type SStream<'i, ...
JohnT's user avatar
  • 115
1 vote
1 answer
119 views

I'm aware it is not much possible nor recommended to have both a field that owns a value, and another one that stores a reference to the same value in a struct. I was experimenting a bit with a more ...
pjsph's user avatar
  • 11
0 votes
1 answer
141 views

Edit: added a second part of the question below. I am getting the error "borrow of moved value" here, but the thing being moved is an &mut Containee, so I didn't expect it to cause a ...
knutaf's user avatar
  • 113
2 votes
1 answer
68 views

I'm trying to create a little utility that simplifies the execution of repetitive work. This would be almost trivial in memory managed languages, but gets unnervingly complex in Rust. I'd call myself ...
NoBullsh1t's user avatar
4 votes
2 answers
189 views

I want to understand lifetimes and the elision rules better. Suppose we want to extend a Vec<i32> (really Vec<&i32>) fn extend_vector( v: &mut Vec<&i32>, x: &...
Markus Klyver's user avatar
5 votes
1 answer
79 views

I have the following code: fn test() -> (impl FnMut(&mut u8), impl FnMut(&mut u8)) { let a = |v: &mut u8| {*v = 0}; let b = move |v: &mut u8| { a(v); println!("{}&...
Nils Oskar Nuernbergk's user avatar
1 vote
2 answers
102 views

I wanted to implement the Merge Sort algorithm, but I am having trouble with the borrow checker rules. The compiler gives me the hint: cannot borrow *lst as immutable because it is also borrowed as ...
Finn's user avatar
  • 105
1 vote
1 answer
66 views

Context I'm trying to learn Rust by just reading the book and working through the problems in Advent of Code (the 2024 edition). Just to make it harder, I decided to practice TDD while I do it. To ...
Juan F. Meleiro's user avatar
1 vote
2 answers
89 views

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
77 views

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,051
0 votes
0 answers
74 views

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.8k
1 vote
0 answers
74 views

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
95 views

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
76 views

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
116 views

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
141 views

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.7k
1 vote
0 answers
118 views

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
  • 141
2 votes
4 answers
406 views

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
172 views

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

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
132 views

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,800
-1 votes
1 answer
69 views

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,318
0 votes
1 answer
74 views

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
0 votes
0 answers
33 views

I have been working on a DOM object in Rust and to insure document integrity I want to implement back references to parent objects, prevent circular references, etc. Minimally my code looks like this: ...
Andrew's user avatar
  • 319
4 votes
1 answer
93 views

I ran into a surprising (to me) error while using RefCell and I want to understand better why this is happening. I had something like this code below where I have a while let block consuming a mutable ...
Tudor's user avatar
  • 157
0 votes
1 answer
71 views

The following doesn't compile as I expected, as y is dropped before r. fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { x } else { y } } fn main()...
Satyam Jay's user avatar
0 votes
0 answers
32 views

MRE: enum MRE_Enum { variant1, variant2, } struct MRE_Struct { dummy1: Vec<MRE_Enum>, dummy2: isize, } // Private functions together impl MRE_Struct { fn foo(&mut self) ...
kesarling's user avatar
  • 2,318
1 vote
2 answers
114 views

I am making a game in Rust and have a few different trait GameModules that are a part of the main App. pub struct AppView<'a> { pub frame_delta: &'a f64, pub area: &'a Rect } ...
sackboy's user avatar
  • 13
0 votes
0 answers
37 views

Sorry, I'm just starting to learn rust, and still don't understand many of its features. I was suprised that this rust code compiles without warning or error: fn main() { let s1 = String::from(&...
Emily Penstemmon's user avatar
2 votes
1 answer
70 views

I want to implement a mutable Iterator for my custom struct as following: #[derive(Debug, Clone)] pub struct Message<'a> { content: &'a str, } #[derive(Debug, Clone)] pub struct ...
Alex's user avatar
  • 49
0 votes
0 answers
53 views

I'm building a client for an API targeting an embedded system. I'm using the reqwless HTTP client library running on top of esp-hal based stack. On top of reqwless, I want to implement automatic ...
Emil Sahlén's user avatar
  • 2,162
1 vote
1 answer
91 views

I'm reading "the book", and wanted to test my understanding of the borrowing rules in chapter slicing. I was impressed with (my assumption of) how the borrow checker associated what I passed ...
Lee's user avatar
  • 1,661
1 vote
1 answer
113 views

Env: rustc 1.82.0 (f6e511eec 2024-10-15) Question: Dereferencing vs. Direct Borrowing in Rust I’m confused about the difference between *&T and T in borrowing. The following code compiles with ...
galaxyzen's user avatar
2 votes
1 answer
104 views

I'm trying to learn Rust. A pattern that I've used in other languages is to wrap an iterator with a data structure that allows me to call .peek() or .next(). The result looks like an iterator, except ...
btilly's user avatar
  • 47.8k
2 votes
1 answer
92 views

I have a struct called UpgradeProperties that takes a Box<dyn FnMut(&mut Self)> as a field. However, when I try to create a function to call it I get an error. Relevant Code: pub struct ...
Andy Galvez's user avatar
1 vote
0 answers
38 views

I have been trying to implement my own Option::get_or_insert_with, with a twist: the function that produces the value to insert in the Option is fallible. This was my first, most reasonable-looking ...
Matteo Monti's user avatar
  • 9,110
0 votes
0 answers
42 views

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 ...
Omega500's user avatar
0 votes
2 answers
82 views

I have the following two external functions: try_borrow_data(&AccountInfo<'a>) -> Result<Ref<&mut [u8]>, ProgramError> read(data: &[u8]) -> Option<&Self> ...
user1939915's user avatar
4 votes
0 answers
61 views

Here is some code that causes my issue as succinctly as possible: struct Parent { child: Option<Box<Parent>>, } fn main() { let mut parent = &mut Parent { child: None }; ...
Lucas Lange's user avatar
0 votes
1 answer
226 views

I'd like to write an async function recv_some which takes a watch::Receiver<Option<T>>, waits for the value to be Some, and returns something which Deref's to T. For reasons I sort of ...
dspyz's user avatar
  • 5,604
0 votes
1 answer
90 views

I have a problem I found a solution to but it is so ugly that there must be a better "idiomatic" way to solve it (I'm from a C++/Java background so I still fell the urge to make everything ...
Johan's user avatar
  • 397
0 votes
0 answers
42 views

In the following code I use the square bracket operator on a HashMap: let mut my_hash_map: HashMap<u32, String> = HashMap::new(); my_hash_map.insert(5, "value".to_string()); let my_val ...
Zebrafish's user avatar
  • 16.3k
0 votes
0 answers
33 views

Basically I am having hard time understanding why calling multiple methods on a mutable reference can cause the borrow checker to err. Example snippet: use std::collections::HashMap; struct MainState ...
Paperino's user avatar
  • 1,019
0 votes
0 answers
48 views

I'm learning Rust and I wrote this somewhat contrived example. #[derive(Debug)] struct Foo<'a> { x: i32, s: &'a String, } impl<'a> Foo<'a> { fn new(s: &String) -&...
Adam's user avatar
  • 1,151
0 votes
1 answer
65 views

I would like to know how to remove all of the outgoing edges for a given node in a directed GraphMap. I've tried this: use petgraph::{graphmap::GraphMap, visit::EdgeRef, Directed, Direction}; fn main()...
Ron Slosberg's user avatar
0 votes
2 answers
121 views

I've been reading the section Unique immutable borrows in captures of the book The Rust Reference and can't figure out why the code is illegal: let mut b = false; let x = &mut b; { let mut c = ...
xm lian's user avatar
  • 107

1
2 3 4 5
38