All Questions
588
questions
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 ...
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);
}
...
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 = ...
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 ...
-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 ...
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 ...
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: &...
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 ...
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:...
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!("{}", ...
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,...
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: ...
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 = !&...
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 ...
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!("{:?}", ...