从 &选项 中获取字符串<String>



我想从configparser::ini::Ini的配置映射中返回&Option<String>的字符串

use configparser::ini::Ini;

fn main() {
let filename : String = "p2pvpn.conf".to_owned();
let mut config = Ini::new();
let map = config.load(filename).unwrap();
let tunc = map.get("tun").unwrap(); //returns hashmap
/*
* 1st unwrap returns &Option<String>
* 2nd unwrap should return String
*/
let tunopt : String  = tunc.get("ip").unwrap().unwrap(); //here is the problem
println!("{tunopt}"); 
}

但是我得到这个错误:

--> src/main.rs:9:28
|
9 |     let tunopt : String  = tunc.get("ip").unwrap().unwrap();
|                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `Option<String>`, which does not implement the `Copy` trait
|
help: consider borrowing the `Option`'s content
|
9 |     let tunopt : String  = tunc.get("ip").unwrap().unwrap().as_ref();
|                                                            +++++++++

我尝试了as_ref选项,但它没有帮助(预期结构String,找到参考),to_string也没有帮助。

我知道get之后的第一个unwrap返回&Option<String>tunc.get("ip").unwrap().unwrap () .

我试过了:

let tunopt : Option<String>  = *(tunc.get("ip").unwrap()); //move this, deference using asterisk

我认为这将所有权但仍不工作

13 |     let tunopt : Option<String>  = *(tunc.get("ip").unwrap()); //move this, deference using asterisk
|                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `Option<String>`, which does not implement the `Copy` trait
|
help: consider borrowing the `Option`'s content

我的问题是:

  1. 如何正确得到选择参考价值String&Option<String>?
  2. 为什么我不能服从&Option<String>并将其所有权移动到Option<String>类型的变量?
  3. 为什么它抱怨这个:not implement the 'Copy' trait',我正试图移动而不是复制。

不能将String移出&Option<String>,句号。事实上,你不能从一个不可变引用(&)后面的对象中移出任何东西,除非该对象具有某种形式的内部可变性(RefCell,Mutex,等等)。这就是不可变借用的意义。

要将String移出config,config必须更新自己,以反映它不再拥有该字符串,并且它不应该再尝试访问它。否则,它很可能在您释放字符串之后尝试访问该字符串,从而导致内存不安全。

configparser::ini::Ini是否有一个方法来删除字符串值并转移其所有权:remove_key。这将允许您将字符串移出Ini,但顾名思义,它是通过从Ini对象中删除字符串来实现的。

或者你可以克隆字符串。配置解析不太可能是程序中的热点,而且这样的字符串通常不是那么大。

不能使用get直接从HashMap中移动值。移动通常意味着你从某处拿走某物。

在这种情况下,您要尝试简单地"get"价值,让你只收到对它的崇敬。要移动它,您可以从HashMap中删除元素,这样您就可以接收值本身。

在实践中你可以这样做:

从hashMap复制一个值:

let map = config.load(filename).unwrap();
let tunc = map.get("tun").unwrap();
let tunopt: &Option<String> = tunc.get("ip").unwrap();
let tunopt: &String = tunopt.as_ref().unwrap();
let tunopt: String = tunopt.to_owned();

或者取值,也就是从HashMap中移除到本地变量

let mut map = config.load(filename).unwrap();
let tunc = map.get_mut("tun").unwrap();
let tunopt: Option<String> = tunc.remove("ip").unwrap();
let tunopt: String = tunopt.unwrap();

您不能将String移出共享引用,但您可以通过使用String上的引用来借用Option的内容:

use configparser::ini::Ini;
fn main() {
let filename: String = "p2pvpn.conf".to_owned();
let mut config = Ini::new();
let map = config.load(filename).unwrap();
let tunc = map.get("tun").unwrap(); //returns hashmap
/*
* 1st unwrap returns &Option<String>
* 2nd unwrap returns &String
*/
let tunopt: &String = tunc.get("ip").unwrap().as_ref().unwrap(); //problem solved
println!("{tunopt}");
}

相关内容

最新更新