Skip to main content
Filter by
Sorted by
Tagged with
5 votes
1 answer
176 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,817
1 vote
1 answer
112 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
2 votes
1 answer
67 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
5 votes
1 answer
75 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
1 answer
62 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
4 votes
1 answer
76 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
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
2 votes
2 answers
70 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
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
2 votes
4 answers
391 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
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
-1 votes
1 answer
68 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,322
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
0 votes
1 answer
68 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
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
2 votes
1 answer
67 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
1 vote
1 answer
90 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
2 votes
1 answer
103 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
1 vote
0 answers
37 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
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
0 votes
1 answer
224 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
0 answers
41 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
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
2 answers
120 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
0 votes
1 answer
45 views

I have the following example code: let mut errors: Vec<String> = Vec::new(); let msg1 = "message 1".to_string(); let msg2 = "message 2".to_string(); let msg3 = "message ...
sudoExclamationExclamation's user avatar

1
2 3 4 5
38