ownership Questions
4
Solved
Consider the following example from the Book:
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!...
Discrepant asked 2/6, 2021 at 9:32
4
Solved
I have to iterate on keys, find the value in HashMap by key, possibly do some heavy computation in the found struct as a value (lazy => mutate the struct) and cached return it in Rust.
I'm getti...
7
Solved
In Rust, there are two possibilities to take a reference
Borrow, i.e., take a reference but don't allow mutating the reference destination. The & operator borrows ownership from a value.
Borr...
Chokebore asked 17/5, 2015 at 15:46
2
Solved
Even after reading the chapters about reference ownership and borrowing, I cannot understand some things in the following code, effectively stopping me from calling more than one method from clap::...
3
Given the following struct (in practice, its bigger than this):
#[derive(Deserialize, Debug)]
struct Parameter {
name: String,
value: String,
}
If I want to create an instance of this (for tests...
1
Solved
This is a question from Rust quiz 28:
struct Guard;
impl Drop for Guard {
fn drop(&mut self) {
print!("1");
}
}
fn main() {
let _guard = Guard;
print!("3");
let _ = ...
3
Solved
To illustrate the necessity of Rc<T>, the Book presents the following snippet (spoiler: it won't compile) to show that we cannot enable multiple ownership without Rc<T>.
enum List {
Co...
Retiarius asked 29/5, 2021 at 4:10
1
I'm decoding a reqwest::Response to JSON. Usually that works fine, but in some rare cases the remote server returns a response that doesn't fit my struct that I'm using for deserialization. In thos...
2
Solved
I'm looking at some code that uses
Rc<RefCell<SomeStruct>>
So I went out to read about the differences between Rc and RefCell:
Here is a recap of the reasons to choose Box, Rc, or RefC...
3
Solved
I have a MainStruct which owns an instance of HelperStruct. I want to make a method that calls helper's method. But the helper's method really needs (immutable) access to MainStruct
struct MainStru...
Moreno asked 29/10, 2022 at 18:17
1
Solved
Interested why does set method defined on Cell, on the last line explicitly drops old value.
Shouldn't it be implicitly dropped (memory freed) anyways when the function returns?
use std::mem;
use s...
Achromatic asked 19/10, 2022 at 10:19
6
Solved
The following code does not compile:
use std::str::Chars;
struct Chunks {
remaining: Chars,
}
impl Chunks {
fn new(s: String) -> Self {
Chunks {
remaining: s.chars(),
}
}
}
The error ...
1
Solved
I'm attempting to build a sort of HTTP web server as a learning exercise and I'm having trouble trying to make one of my types iterable using a for loop (by implementing IntoIterator).
So far
I've ...
Handedness asked 23/8, 2022 at 18:19
1
Solved
Consider the following example code:
#[derive(Clone)]
struct DataRef<'a> {
text: &'a str,
}
#[derive(Clone)]
struct DataOwned {
text: String,
}
I am going to implement ToOwned for Dat...
2
Solved
I have never experimented with languages like C, C++, Go, etc., and I decided to start with Rust, I already understand a little what the stack and the Heap are, but, what does it really mean by mov...
Jurkoic asked 22/3, 2022 at 17:49
1
The minimal example is this (Playground):
#[derive(Debug)]
struct Node {
id: usize,
}
fn main() {
let mut node = Node { id: 0 };
let mut lastnode = &mut node;
let mut last = lastnode; // m...
2
Solved
I am new to Rust. I need to create a vector before a for loop. Run for loop on it. Change the vector inside the for loop. Then Change the vector after the for loop.
I tried the following code and...
Resentment asked 24/8, 2019 at 11:3
1
Solved
So far, I've seen two builder patterns in official Rust code and other crates:
impl DataBuilder {
pub fn new() -> DataBuilder { ... }
pub fn arg1(&mut self, arg1: Arg1Type) -> &mut ...
Malaco asked 19/12, 2021 at 1:7
1
This is my first project in Rust, and I think I'm missing something simple.
I'm attempting to create a simple Web API daemon that will receive POSTs of JSON, parse the JSON, and send an email usi...
Allpowerful asked 24/11, 2019 at 1:36
2
Solved
I'm trying to learn Rust's ownership and lifetimes in more detail, and I'm very confused by this piece of code:
let mut lst = vec![1, 2, 3];
let mut x = &mut 0;
for value in &mut lst {
*v...
1
Solved
I want to iterate over the bytes of an integer:
use core::mem::size_of;
const SIZE: usize = size_of::<u64>();
fn main() {
let x: u64 = 512;
let mut buf: [u8; SIZE] = [0; SIZE];
...
1
Taking example snippets from here: the following doesn't compile
fn foobar<F>(mut f: F)
where F: FnMut(i32) -> i32
{
println!("{}", f(f(2)));
// error: cannot borrow `f` as m...
Cluck asked 26/9, 2021 at 20:56
2
Solved
I've just written a small Rust program which calculates Fibonacci numbers and memoizes the calculation. It works, but I'm a little confused about why, especially the recursive call. (It also probab...
2
Solved
I have this if statement where both branches have to return a &HashMap<..>. In one of the branches though I have an owned HashMap and the other one I'm accessing one from a reference to s...
4
Solved
I have a struct that I want to take by value, mutate and then return. I want to also mutate its generic type as I use this state for statically ensuring correct order of function calls for making s...
Incept asked 19/7, 2021 at 16:41
1 Next >
© 2022 - 2024 — McMap. All rights reserved.