我在阅读这篇文章时遇到了这种奇怪的行为,这篇文章的核心问题是当你匹配时(&k, &v) = &(&String, &String),k会v得到类型String。
为了弄清楚发生了什么,我编写了以下测试代码,结果让我更加震惊和困惑:
fn main() {
let x: &(&String, &String) = &(&String::new(), &String::new());
let ref_to_x: &&(&String, &String) = &x;
let ref_ref_to_x: &&&(&String, &String) = &&x;
let ref_ref_ref_to_x: &&&&(&String, &String) = &&&x;
// code snippet 1
let (a, b) = x; // type of a: &&String, type of b: &&String
let (a, b) = ref_to_x; // type of a: &&String, type of b: &&String
let (a, …Run Code Online (Sandbox Code Playgroud) 我正在尝试修剪并小写我的字符串。
目前我有
use dialoguer::Input;
let input: String = Input::new()
.with_prompt("Guess a 5 letter word")
.interact_text()
.unwrap();
let guess: &str = input.as_str(); // trim and lowercase
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其转换String为修剪后的小写字母&str,但某些功能仅打开&str,而其他功能仅打开String,因此我无法想出一个优雅的解决方案。
TL;DR:最终目标必须guess是修剪后的小写字母&str