我正在尝试得到这样的东西(不起作用):
match input {
"next" => current_question_number += 1,
"prev" => current_question_number -= 1,
"goto {x}" => current_question_number = x,
// ...
_ => status = "Unknown Command".to_owned()
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两个不同版本的Regex:
go_match = regex::Regex::new(r"goto (\d+)?").unwrap();
// ...
match input {
...
x if go_match.is_match(x) => current_question_number = go_match.captures(x).unwrap().get(1).unwrap().as_str().parse().unwrap(),
_ => status = "Unknown Command".to_owned()
}
Run Code Online (Sandbox Code Playgroud)
和
let cmd_match = regex::Regex::new(r"([a-zA-Z]+) (\d+)?").unwrap();
// ...
if let Some(captures) = cmd_match.captures(input.as_ref()) {
let cmd = captures.get(1).unwrap().as_str().to_lowercase();
if let Some(param) = captures.get(2) { …Run Code Online (Sandbox Code Playgroud) 从&Result类型中提取数据的好方法是什么?
在我的具体情况下,我有一个&Result<DirEntry, Error>类型,我无法解开因为我没有拥有该对象.我尝试取消引用并克隆它(*left_item).clone(),但这只是给我一个错误的注释:
the method `clone` exists but the following trait bounds were not satisfied:
`std::result::Result<std::fs::DirEntry, std::io::Error> : std::clone::Clone`
Run Code Online (Sandbox Code Playgroud) 我有一个match声明,它返回一个&str:
match k {
SP_KEY_1 => "KEY_1",
SP_KEY_2 => "KEY_2",
SP_KEY_3 => "KEY_3",
SP_KEY_4 => "KEY_4",
SP_KEY_5 => "KEY_5",
SP_KEY_6 => "KEY_6",
_ => (k as char), // I want to convert this to &str
}.as_bytes()
Run Code Online (Sandbox Code Playgroud)
我试图char先将一个字符串转换为字符串,然后再将其切换为:
&(k as char).to_string()[..]
Run Code Online (Sandbox Code Playgroud)
但这给了我一生的错误:
error[E0597]: borrowed value does not live long enough
Run Code Online (Sandbox Code Playgroud)
说明这(k as char).to_string() 是一个临时值,从我能说的内容来看是有意义的,它会to_string()返回一个克隆.
我可以添加.to_string()到&str上面的每个文字来产生返回值String,但这看起来既丑陋(很多重复的代码),并且可能是低效的,因为to_string()克隆了原始的字符串切片.
具体问题是我将如何char进入a &str,但更广泛的问题是有更好的解决方案,这种情况通常是做什么的.
我试图让 &str 和 &str 在 for 循环中连接,目的是在添加了许多部分后使用新的组合字符串。for 循环的一般布局如下所示,但由于大量错误,我在组合字符串时遇到了很多麻烦。
for line in reader.lines() {
let split_line = line.unwrap().split(",");
let mut edited_line = "";
for word in split_line {
if !word.contains("substring") {
let test_string = [edited_line, word].join(",");
edited_line = &test_string;
}
}
let _ = writeln!(outfile, "{}", edited_line).expect("Unable to write to file");
}
Run Code Online (Sandbox Code Playgroud)
第一个错误:
error[E0716]: temporary value dropped while borrowed
Run Code Online (Sandbox Code Playgroud)
运行上面的时候来。
第二个错误:
error[E0308]: mismatched types expected &str, found struct std::string::String
Run Code Online (Sandbox Code Playgroud)
当您从 test_string 中删除 & 并将其分配给edited_line 时发生
注意:format!和concat!宏都给出错误 2。 …
是否有类似于?快捷方式的东西,它在出现错误时不是从函数返回结果,而是返回预定义的值?
基本上我想知道是否可以在一行中执行以下操作:
fn index() -> String {
let temp = some_func("pass"); // some_func returns a Result
if temp.is_err() {
return "No parameters named pass".to_string();
}
try_decrypt_data(temp.unwrap())
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用unwrap_or_else(),但这只是返回闭包而不是外部函数。IE
try_decrypt_data(params.get("pass").unwrap_or_else(|| return "No parameters named pass".to_string(); )) // This doesn't work
Run Code Online (Sandbox Code Playgroud) 如何将Uint32数字转换为 4 个字节(如 aUint8List或类似的)。
上下文:我正在使用该image包并调用getPixel(x,y)函数,该函数返回一个Uint32. 我想将它分离到 rgba 颜色通道中。
有没有办法让文本框和按钮成为焦点?我知道我可以textbox1.focus用来聚焦文本框,和按钮一样,但有没有办法能够在按钮上输入焦点,同时仍然在文本框中输入?
我知道我可以使用按键事件来捕获回车键,但我想知道是否可以将按钮输入焦点.
只是想知道这是否可能,只需要一个简单的是或否.谢谢!
目前,我有一个类(classB)有一个列表--v
class classB
{
public List<int> alist { get; private set; }
...
Run Code Online (Sandbox Code Playgroud)
我可以从另一个类(classA)访问此类
ClassB foo = new ClassB();
foo.alist.Add(5);
Run Code Online (Sandbox Code Playgroud)
问题是,我可以阻止这种情况发生吗?(让其与其他课程无法改变)谢谢!
我试图弄清楚如何在 Rust 内部设置全局 Windows 挂钩。我可以找到其他语言的多个示例,但 Rust 似乎没有任何示例。
到目前为止我所取得的成果:
extern crate user32;
extern crate winapi;
const WH_KEYBOARD_LL: i32 = 13;
fn main() {
let hook_id = user32::SetWindowsHookExA(
WH_KEYBOARD_LL,
Some(hook_callback),
// No idea what goes here ,
0,
);
}
fn hook_callback(code: i32, wParam: u64, lParam: i64) -> i64 {
// ...
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨它需要一个fn用于回调函数的“系统”,但是得到了一个 Rust fn,这是有道理的,但我仍然不知道如何使其工作。
从我从文档中收集到的信息来看,第三个参数hMod 应该指向具有回调函数的同一个模块,其他语言中的示例使用一些获取当前模块句柄的函数,但我不知道如何在锈。
我目前的解决方案是:
let temp = format!(
"{}.png",
path.file_stem().unwrap().to_string_lossy());
path.pop();
path.push(&temp);
Run Code Online (Sandbox Code Playgroud)
这很丑陋,需要至少 6 个函数调用并创建一个新字符串。
有没有更惯用、更短或更有效的方法来做到这一点?
这似乎非常简单,但我似乎找不到一种方法来做到这一点。基本上就是标题,我想找到列表中满足要求的第一个项目,并修改找到的项目的值,如果该列表中没有一个项目满足要求,则执行其他操作。
我使用了 foreach 循环,但这绝对不是最快的方法。
foreach (CustomClass foo in bar)
{
if (!foo.Value)
{
foo.Value = true;
currentCount++;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
List.First()然后,当找不到值时,我尝试使用并捕获异常,但这要慢得多,而且我正在寻找性能。
编辑:不要介意下面的内容,我找到了如何使第一个或默认工作,但是有没有比 foreach 方法更快的方法多次执行此操作?谢谢
所以我尝试了 FirstOrDefault,但我不断收到空引用异常
if (bar.FirstOrDefault(c => c.Value == false).Equals(null))
{
break;
}
else
{
thePicture.FirstOrDefault(c => c.Value == false).Value = true;
currentCount++;
}
Run Code Online (Sandbox Code Playgroud)
有人知道如何使第一个或默认工作吗?或者有没有比 foreach 方法更快的其他方法。(这将在另一个循环中运行很多次)谢谢!