ownership

Method call on clap::App moving ownership more than once

戏子无情 提交于 2020-01-15 07:48:28
问题 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::App ! extern crate clap; use clap::App; fn main() { let mut app = App::new("name me").args_from_usage("<input_file> 'Sets the input file to use'"); let matches = app.get_matches(); app.print_help(); println!( "Using input file: {}", matches.value_of("input_file").unwrap() ); } Compiling this code

Method call on clap::App moving ownership more than once

柔情痞子 提交于 2020-01-15 07:48:25
问题 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::App ! extern crate clap; use clap::App; fn main() { let mut app = App::new("name me").args_from_usage("<input_file> 'Sets the input file to use'"); let matches = app.get_matches(); app.print_help(); println!( "Using input file: {}", matches.value_of("input_file").unwrap() ); } Compiling this code

What is the right way to expose resources owned by a class?

会有一股神秘感。 提交于 2020-01-12 12:09:49
问题 Let's say I have a library which has a Document class. An instance of Document can own several instances of Field . Field has multiple subclasses (for example IntegerField and StringField ), and even the API user can subclass it and supply subclass instances to Document (let's say the user is allowed to develop a custom type of data to store in a field). I'd like to expose the Field instances owned by Document through an API in such a way that users can interact with them, but without

What is the right way to expose resources owned by a class?

天大地大妈咪最大 提交于 2020-01-12 12:09:34
问题 Let's say I have a library which has a Document class. An instance of Document can own several instances of Field . Field has multiple subclasses (for example IntegerField and StringField ), and even the API user can subclass it and supply subclass instances to Document (let's say the user is allowed to develop a custom type of data to store in a field). I'd like to expose the Field instances owned by Document through an API in such a way that users can interact with them, but without

How is a destructor call `fn drop(&mut self)` call inserted when the owning variable is immutable?

社会主义新天地 提交于 2020-01-03 11:37:20
问题 It is my understanding that when a variable whose type implements Drop goes out of scope, a call to the fn drop(&mut self) function is inserted, and passed a newly-created mutable reference to the variable going out of scope. However, how is that possible in cases where the variable was immutably bound, and it would be illegal to borrow it mutably? Here's an example of what I'm talking about: fn main() { let x = vec![1, 2, 3]; let y = &mut x; } Which produces the following error: cannot

cpanel change ownership of files

帅比萌擦擦* 提交于 2020-01-03 06:08:08
问题 I'm in a totally new situation. I have the access on root on a reseller account. One of the clients for that reseller has a file, he can't modify. Is a file installed with a plugin in wordpress. That much I understood. He is not the owner of the file. I have to change the owner of that file. I have shell acces and cron acces but I was not able to use it to solve the problem. The solution I come out till now, that doesn't work is adding a new cron job(copy and paste from a forum) #!/bin/bash

How to convert Option<&T> to Option<T> in the most idiomatic way in Rust?

本小妞迷上赌 提交于 2020-01-02 02:43:05
问题 When using HashMap's get method, I get an Option<&T> , I've encountered it again this time with Option<&String> . I'd like to get an owned value Option<String> . Is this possible without me writing map(|x| x.to_owned()) ? I'm just wondering if there's a way to write a blanket implementation for any of the utility traits to achieve that? 回答1: Option comes with utility methods for various transformations, which are listed in its documentation. For any T that implements Clone (which String does)

Syntax guidelines for taking ownership and releasing objects in C++

人走茶凉 提交于 2020-01-01 03:14:07
问题 I want to know - are there any guidelines about syntax of C++ (non-)member functions, that allows me to understand (without comments, if possible) the ownership policy of its arguments and return value. By ownership I mean, that the owner is responsible for the destruction of the owned object. I distinguish the following rules about arguments: take ownership don't take ownership share and about return value: release ('return by value' is in this group) don't release share For example, passing

Why do I not need to explicitly lend a borrowed, mutable variable?

微笑、不失礼 提交于 2019-12-30 17:43:52
问题 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 probably isn't idiomatic.) Here's the program: use std::collections::HashMap; fn main() { let n = 42; // hardcoded for simplicity let mut cache = HashMap::new(); let answer = fib(n, &mut cache); println!("fib of {} is {}", n, answer); } fn fib(n: i32, cache: &mut HashMap<i32,i32>) -> i32 { if cache

How to borrow an unwrapped Option<T>? [duplicate]

半城伤御伤魂 提交于 2019-12-25 01:26:45
问题 This question already has an answer here : Cannot move out of borrowed content when unwrapping (1 answer) Closed last year . I want to iterate over a vector using .iter_mut() and .map() : fn calculate_distances(planes : &mut Vec<Aeroplane>, x: f64, y: f64) { fn calculate_distance(x1: &f64, y1: &f64, x2: &f64, y2: &f64) -> f6 { ... } planes.iter_mut().map(|a| if a.position.is_some() { let pos: &Position = &a.position.unwrap(); a.distance = Some(calculate_distance(&x, &y, &pos.latitude, &pos