borrowing Questions

3

Solved

I am trying to use the newtype pattern to wrap a pre-existing type. That inner type has a modify method which lets us work with a borrowed mutable value in a callback: struct Val; struct Inner(Va...
Genaro asked 1/1, 2019 at 22:59

3

Solved

I want to write a single function, that accepts a &str, a String and a borrowed &String. I've written the following 2 functions: fn accept_str_and_ref_string(value: &str) { println!("...
Geelong asked 9/3, 2019 at 15:50

2

Solved

I'm attempting to store a reference to an element of a mutable vector to use later. However, once I mutate the vector, I can no longer use the stored reference. I understand that this is because bo...
Minter asked 16/2, 2021 at 5:44

2

I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `...
Jilleen asked 1/12, 2019 at 7:27

2

Solved

I have a struct like this: #[derive(Serialize, Deserialize)] struct Thing { pub small_header: Header, pub big_body: Body, } I want to serialize this Thing to send over the network. I already h...
Swinge asked 10/10, 2018 at 5:15

2

Solved

I'm trying to convert a HashSet<String> into a sorted vector that can then be joined with commas: use std::collections::HashSet; fn main() { let mut hs = HashSet::<String>::new(); h...
Besnard asked 27/3, 2020 at 19:37

2

Solved

I'd like to create an iterator that for this input: [1, 2, 3, 4] Will contain the following: (1, 2) (2, 3) (3, 4) Peekable seems ideal for this, but I'm new to Rust, so this naïve version doesn't...
Puttyroot asked 17/2, 2021 at 0:15

1

Solved

I am building a simple command-line todo app in Rust. If I don't implement the copy trait I get this error: "move occurs because 'todo' has type 'todo::Todo', which does not implement th...
Ogham asked 12/1, 2021 at 2:57

3

Solved

fn main() { let strA = "a"; let result; { let strB = "abc"; result = longest(strA, strB); // Will return strB } println!("The longest string is {}", res...
Supraorbital asked 27/11, 2020 at 15:46

2

Solved

When writing a function how does one decide whether to make input parameters referenced or consumed? For example, should I do this? fn foo(val: Bar) -> bool { check(val) } // version 1 Or use r...
Shilohshim asked 13/11, 2020 at 0:32

2

Solved

I've seen the Borrow trait used to define functions that accept both an owned type or a reference, e.g. T or &T. The borrow() method is then called in the function to obtain &T. Is there so...
Tiki asked 18/8, 2020 at 9:25

1

Solved

The Rust book talks about having multiple readers and multiple mutable references to an object as a data race situation that may lead to issues. For example, this code: fn main() { let mut...
Ober asked 17/5, 2020 at 12:38

2

Solved

I'm implementing a stack-like structure, where the structure holds a mutable reference to a slice. struct StackLike<'a, X> { data: &'a mut [X], } I'd like to be able to pop the last ...
Entangle asked 15/4, 2020 at 7:12

2

This is how the str type is used: let hello = "Hello, world!"; // with an explicit type annotation let hello: &'static str = "Hello, world!"; let hello: str = "Hello, world!"; leads t...
Blimp asked 12/4, 2020 at 16:8

2

Solved

Why would all these three print_max functions work? Which one is the best practice? Is for number in number_list a shortcut for for number in number_list.iter()? fn main() { let number_list = vec...
Consueloconsuetude asked 29/2, 2020 at 2:29

2

Solved

I've read these docs: https://doc.rust-lang.org/rust-by-example/scope/borrow/mut.html I've also read this question: (Cannot borrow immutable borrowed content as mutable) The docs helped me unde...
Churrigueresque asked 20/2, 2020 at 16:33

2

Solved

I'm trying to parse a file and return Vec<Vec<&str>> from a function. But I'm getting borrowed value error inside the file read loop while pushing to the vector. use std::io::{self...
Johore asked 24/11, 2019 at 20:45

1

Solved

I am reading the Rust Book and everything was pretty simple to understand (thanks to the book's authors), until the section about lifetimes. I spent all day, reading a lot of articles on lifetimes ...
Hardshell asked 9/11, 2019 at 19:48

1

Solved

First of all I'm new with Rust :-) The problem: I want to create a module called RestServer that contain the methods ( actix-web ) to add routes and start the server. struct Route { url: String,...
Piles asked 28/10, 2019 at 23:14

1

Solved

Why it is allowed to do something like this: fn main() { let mut w = MyStruct; w.fun1(); } struct MyStruct; impl MyStruct { fn fun1(&mut self) { self.fun2(); } fn fun2(&mut self) ...
Mastership asked 13/10, 2019 at 19:41

2

Solved

I have the following minimal example of my code: fn main() { let names : Vec<Vec<String>> = vec![ vec!["Foo1".to_string(), "Foo2".to_string()], vec!["Bar1".to_string(), "Bar2".to_st...
Pontoon asked 21/1, 2019 at 10:15

1

Solved

I understand that a borrow cannot outlive the existence of the thing it points to, to eradicate the dangling pointers. A borrow or an alias can outlive the owner by faking the lifetimes: fn main(...
Hazard asked 9/1, 2019 at 17:28

1

Solved

I am learning Rust from The Rust Programming Language book available from No Starch Press but ran into an issue where the compiler did not behave as explained in the book in chapter 4 on p. 77. Ch...
Ipsambul asked 28/12, 2018 at 5:29

4

Solved

I have two variables of type &T, x and y, which I swap locally inside a function: pub fn foo<T: Copy>(mut x: &T) { let y_owned = *x; let mut y = &y_owned; for _ in 0..10 { do...
Fishnet asked 18/12, 2018 at 14:56

1

Solved

Rust has the concepts of ownership and borrowing. If a function doesn't borrow its parameter as a reference, the arguments to that function are moved and will be deallocated once they go out of sco...
Mal asked 16/12, 2018 at 15:59

© 2022 - 2024 — McMap. All rights reserved.