ownership Questions
2
I am looking for a function similar to slice::split_at_mut. Let's name it split_at with the signature
pub fn split_at<T>(v: Vec<T>, mid: usize) -> (Vec<T>, Vec<T>)
such ...
Bradfordbradlee asked 29/6, 2021 at 13:35
1
Solved
I am learning Rust and chapter 8.2 of the Rust Programming Language book raised some doubts:
let mut s1 = String::from("foo");
let s2 = "bar";
s1.push_str(s2);
println!("s...
3
Solved
From the book:
Rust won’t let us annotate a type with the Copy trait if the type, or any of its parts, has implemented the Drop trait. If the type needs something special to happen when the val...
Rossuck asked 6/8, 2018 at 9:5
1
Presume we have the following code, where I defined a closure named closure. Within this closure, I want to use the outer x by immutable reference (&T), and at the same time, using y by taking ...
1
Solved
I found it confusing that &(*&a) and &{*&a} behave differently.
To be detailed, the following code failed to compile:
struct CanNotCopy;
fn main(){
let a = CanNotCopy;
&...
1
When I try to extend a HashSet<String> with another HashSet<String>:
use std::collections::HashSet;
let mut a = HashSet::new();
a.insert("foo".to_owned());
let mut b = HashSet...
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...
1
I encountered an error when trying to use a function get_even_numbers() to borrow a vec v by passing it in by reference &v instead of by value v.
fn get_even_numbers(v: &Vec<i32>) -&g...
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...
1
fn main() {
let mut name = String::from("Charlie");
let x = &mut name;
let y = x; // x has been moved
say_hello(y);
say_hello(y); // but y has not been moved, it is still usable
...
Repute asked 17/7, 2020 at 19:25
2
I'm trying to take ownership of a directory with the following code:
sd = win32security.SECURITY_DESCRIPTOR()
sd.SetSecurityDescriptorOwner(curUser, False)
win32security.SetFileSecurity("C:/Progra...
Stace asked 10/12, 2016 at 23:6
7
Solved
I need a function or method in Python to find the owner of a file or directory.
The function should be like:
>>> find_owner("/home/somedir/somefile")
owner3
Northerly asked 2/12, 2009 at 4:19
3
I'm tring to replace a value in a mutable borrow; moving part of it into the new value:
enum Foo<T> {
Bar(T),
Baz(T),
}
impl<T> Foo<T> {
fn switch(&mut self) {
*self = m...
Scullery asked 10/4, 2015 at 21:12
1
Solved
My goal was to implement the suggested improvement on the cacher struct of the rust book chapter 13.1, that is creating a struct which takes a function and uses memoization to reduce the number of ...
2
Does the variable s in print_struct refer to data on the heap or on the stack?
struct Structure {
x: f64,
y: u32,
/* Use a box, so that Structure isn't copy */
z: Box<char>,
}
fn main(...
Rosie asked 7/1, 2020 at 12:12
2
I often encounter a situation when I have complex class (e.g. implementing some numerical algorithm like Partial Differential Equation Solver) with data arrays which it can either own or bind from ...
Zareba asked 3/12, 2019 at 12:30
1
Solved
I am having a problem adding a file to an image and setting ownership via --chown flag. Specifically, here is a dockerfile adding a simple text file:
FROM fedora:24
ARG user_name=slave
ARG user_u...
1
Solved
Consider the following code - which compiles and works:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wr...
Preinstruct asked 13/9, 2019 at 17:44
1
Solved
I'm calling a C constructor function keyNew that allocates memory for a Key struct and returns a *mut Key to the Rust side. Is it appropriate to use Box::from_raw to wrap the pointer and take owner...
1
Solved
According to The Rust book:
Each value in Rust has a variable that’s called its owner. There can be only one owner at a time. When the owner goes out of scope, the value will be dropped.
Accor...
Salyers asked 26/7, 2019 at 18:3
1
Solved
I would like to loop through a range of values that have a BigUint type (from the num crate).
How can I do this?
I tried
for i in 0..a {...}
where a is a (borrowed) BigUint type. I got a error...
5
Solved
I wrote a function which has this form:
Result f( const IParameter& p);
My intention is that this signature will make it clear that the function is not taking ownership of the parameter p.
P...
4
Solved
I'm reading Xcode's documentation, and here is something that puzzles me:
__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
[tmpSelf doSomething];
}];
The following is copied...
Azotobacter asked 2/8, 2012 at 8:2
1
In his February 2018 note titled "Memory Safety in Rust:
A Case Study with C", Will Crichton wrote:
Rust provides the ability to take ownership of raw pointers, which we do using slice::from_ra...
Medium asked 24/2, 2019 at 8:1
© 2022 - 2024 — McMap. All rights reserved.