Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
0 votes
1 answer
84 views

How can I pass a reference to a closure in Rust?

I am using wry to spawn a few web views. Each view has a on_page_load_handler. Inside the handler I need to access the web view to e.g. navigate to another website. Sadly I fail to pass the web view ...
user3563584's user avatar
3 votes
1 answer
101 views

Why does `Option::map` work with immutable data, but not with mutable data

Why does the immutable version in the following example work, but the mutable version does not compile with error error: lifetime may not live long enough? trait Car { fn honk(&self); } ...
puaaaal's user avatar
  • 330
1 vote
1 answer
50 views

A question on Rust trait/struct lifetime bounds coercion

I have recently stumbled upon a lifetime coercion issue that I am unable to resolve. The minimal reproducible example which I think stems from the same problem is the following: const MACGUFFIN: i32 = ...
Christoff van Zyl's user avatar
0 votes
1 answer
100 views

Why does dereferencing a String (not &String) work in Rust?

In the following code, in calculate_length function we are sending String not &String, yet dereferencing works. I expected to get error something: type String cannot be dereferenced But the code ...
luffy's user avatar
  • 117
-1 votes
1 answer
65 views

What effect does the placement of the reference symbol (ampersand) have in a simple loop?

I just started learning rust and I am confused about how the ampersand placement affects the program in the following examples. I understand that '&' indicates a reference to a variable, but I'm ...
Nack's user avatar
  • 1
0 votes
2 answers
99 views

What is the difference between &mut Foo and *mut Foo? [duplicate]

In my Rust program, I'm getting back values of the form *mut Foo from a function written in C. let x: *mut Foo = my_ffi_function(); What does this "*mut" notation even mean? As far as I can ...
ccleve's user avatar
  • 15.6k
0 votes
1 answer
48 views

Why isn't reborrow occured when using it with closures?

Here is the example: fn bar<T>(v: &mut Vec<T>) { bar(v); //reborrows bar(v); //reborrows } which compiles fine. But consider slightly modified version: fn foo<T>(v: &...
Some Name's user avatar
  • 9,347
3 votes
1 answer
34 views

How get an immutable Pin from a mutable Pin?

How to apply deref coercion from &mut to & for references that are wrapped inside Pin<>? That is, how to borrow Pin<&mut _> as Pin<&_>? use std::pin::{Pin, pin}; fn ...
tolvanea's user avatar
0 votes
0 answers
33 views

What does &mut is doing in Rust [duplicate]

I’m new to Rust and come from a C++ background. While playing with some leet questions I found my self with this scenario struct MyStruct; pub fn foo(mut my_box: Box<MyStruct>) { let mut ptr:...
juanphm's user avatar
1 vote
0 answers
43 views

Why isn't into_iter move the value when called on reference [duplicate]

Consider the following example: fn print_ref(vec: &mut Vec<String>) { vec.into_iter().for_each(|v| println!("{}", v)); vec.into_iter().for_each(|v| println!("{}", ...
St.Antario's user avatar
1 vote
0 answers
74 views

Methods returning a reference and trait implementation in Rust

Question rust returning a reference to a integer from a function was tagged as duplicate, and almost is. The question Why can I return a reference to a local literal but not a variable? was referenced,...
FreD's user avatar
  • 492
0 votes
1 answer
46 views

How to return a reference with lifetime from RefCell? [duplicate]

For example, I have the next structure: struct MyStruct { items: RefCell<HashMap<i32, SomeStruct>>, } impl MyStruct { fn new() -> Self { Self { items: ...
Denis Steinman's user avatar
2 votes
1 answer
65 views

Rust why does operator applied to a reference coerce, but let with an explicit type does not?

Consider the following code: fn main() { let foo = 1; let bar: i32 = -&foo; //ok, -1 let baz: i32 = &foo; //error, expected `i32`, found `&{integer}` let bad: i32 = !&...
Some Name's user avatar
  • 9,347
0 votes
1 answer
71 views

Bad design for Rust program with lifetime and multiple references

I am implementing a graph data structure in Rust. For each node (and also edges - omitted from this snippet) in the graph I have a struct Algorithm (perhaps poorly named) that performs some node and ...
RedPen's user avatar
  • 261
4 votes
1 answer
290 views

How to handle "reference to packed field is unaligned" error?

I am revisiting old code and am now encountering an error (synthetic recreation): error[E0793]: reference to packed field is unaligned --> src/main.rs:9:22 | 9 | println!("{:?}", ...
kmdreko's user avatar
  • 55.5k

15 30 50 per page
1
2 3 4 5
40