有时我会重新分配参数绑定,因为不再需要原始绑定,可能会导致混淆.处理这个问题的惯用方法是什么?例如:
fn foo(s: &str) {
let s = s.trim();
}
Run Code Online (Sandbox Code Playgroud)
要么
fn foo(mut s: &str) {
s = s.trim();
}
Run Code Online (Sandbox Code Playgroud) 我有一个foo使用Clap来处理命令参数解析的程序。foo调用另一个程序,bar. 最近,我决定用户如果愿意的话foo应该能够传递参数。bar我将bar命令添加到 Clap 中:
let matches = App::new("Foo")
.arg(Arg::with_name("file").value_name("FILE").required(true))
.arg(
Arg::with_name("bar")
.value_name("[BAR_OPTIONS]")
.short("b")
.long("bar")
.multiple(true)
.help("Invoke bar with these options"),
)
.get_matches();
Run Code Online (Sandbox Code Playgroud)
当我尝试将命令传递"-baz=3"给bar这样的情况时:
let matches = App::new("Foo")
.arg(Arg::with_name("file").value_name("FILE").required(true))
.arg(
Arg::with_name("bar")
.value_name("[BAR_OPTIONS]")
.short("b")
.long("bar")
.multiple(true)
.help("Invoke bar with these options"),
)
.get_matches();
Run Code Online (Sandbox Code Playgroud)
或者
./foo -b -baz=3 file.txt
Run Code Online (Sandbox Code Playgroud)
clap返回此错误:
./foo -b "-baz=3" file.txt
Run Code Online (Sandbox Code Playgroud)
如何通过 Clap 传送命令?
有没有办法对 a 中的元素进行索引BitVec?我想要这样的事情:
s = Solver()
x = BitVec('x', 8)
s.add(Not(And(x[0], x[2])))
Run Code Online (Sandbox Code Playgroud)
或者屏蔽是隔离位的唯一方法:
s.add(x & 5 != 5)
Run Code Online (Sandbox Code Playgroud) 我有一个独特的场景,我想在其中f64用作HashMap. 特别是我知道f64永远不会NaN并且我可以容忍f64应该相等但不是。所以,我transmute()的f64到u64。然而,当我拉u64出来HashMap和transmute()这回f64它是一个不同的值。下面和操场上的代码。
use std::collections::HashMap;
fn main() {
let x = 5.0;
let y: u64 = unsafe { std::mem::transmute(x) };
let x: f64 = unsafe { std::mem::transmute(y) };
println!("y: {}, x: {}", y, x);
let mut hash = HashMap::new();
hash.insert(y, 8);
for (y, _) in &hash {
let x: f64 = unsafe { std::mem::transmute(y) }; …Run Code Online (Sandbox Code Playgroud) 我有一个Vec<T>与模式匹配的元素.我想删除与模式匹配的元素的所有尾随实例.
例如,我有一个Vec<i32>和模式是(|x| x == 0).如果输入为:vec![0, 1, 0, 2, 3, 0, 0],则输出应为:vec![0, 1, 0, 2, 3]
为此,我试过:
fn main() {
let mut vec = vec![0, 1, 0, 2, 3, 0, 0];
vec = vec.into_iter().rev().skip_while(|&x| x == 0).rev();
}
Run Code Online (Sandbox Code Playgroud)
但我得到这些编译器错误:
error[E0277]: the trait bound `std::iter::SkipWhile<std::iter::Rev<std::vec::IntoIter<{integer}>>, [closure@src/main.rs:3:44: 3:55]>: std::iter::DoubleEndedIterator` is not satisfied
--> src/main.rs:3:57
|
3 | vec = vec.into_iter().rev().skip_while(|&x| x == 0).rev();
| ^^^ the trait `std::iter::DoubleEndedIterator` is not implemented for `std::iter::SkipWhile<std::iter::Rev<std::vec::IntoIter<{integer}>>, [closure@src/main.rs:3:44: …Run Code Online (Sandbox Code Playgroud) 我正在生成一个线程来做一些工作。有时我希望这个线程在工作完成后死亡,其他时候我希望它等待更多的工作去做。为此,我传入了一个Option<Receiver<T>>. 如果Option<Receiver<T>>是None线程应该死,否则就应该等待接收更多的工作。
fn foo(rx: Option<Receiver<usize>>) {
thread::spawn(move || {
loop {
do_some_work();
if let Some(r) = rx {
match r.recv() {
Ok(x) => {}
Err(_) => panic!("Oh no!"),
}
} else {
break; //Die
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
(链接到操场)
编译器说:
fn foo(rx: Option<Receiver<usize>>) {
thread::spawn(move || {
loop {
do_some_work();
if let Some(r) = rx {
match r.recv() {
Ok(x) => {}
Err(_) => panic!("Oh no!"),
}
} else {
break; //Die …Run Code Online (Sandbox Code Playgroud) 有没有内置的方法来提供多个键来打破排序?就像是:
vec.sort_by_key(|k| foo(k), bar(k));
foo(k)第一个键在哪里,bar(k)是第二个(打破平局)键?
我有一个包含唯一ID的结构,并使用该ID作为其哈希值:
use std::borrow::Borrow;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
type Id = u32;
#[derive(Debug, Eq)]
struct Foo {
id: Id,
other_data: u32,
}
impl PartialEq for Foo {
fn eq(&self, other: &Foo) -> bool {
self.id == other.id
}
}
impl Hash for Foo {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
impl Borrow<Id> for Foo {
fn borrow(&self) -> &Id {
&self.id
}
}
Run Code Online (Sandbox Code Playgroud)
我了解Foo::id一旦将值放入,就无法修改它的值,HashSet因为那样会改变哈希值。但是,我想修改Foo::other_data。我知道我可以将其从中删除HashSet,进行修改,然后再次插入,但是这样的方法get_mut()会更加干净。有没有办法完成这样的事情:
fn main() …Run Code Online (Sandbox Code Playgroud) 我需要计算向量的长度,(bool, i32)如果为bool真,我会增加计数。我正在使用折叠来执行此操作:
fn main() {
let domain = [(true, 1), (false, 2), (true, 3)];
let dom_count = domain.iter()
.fold(0, |count, &(exists, _)| if exists {count + 1});
println!("dom_count: {}", dom_count);
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨道:
fn main() {
let domain = [(true, 1), (false, 2), (true, 3)];
let dom_count = domain.iter()
.fold(0, |count, &(exists, _)| if exists {count + 1});
println!("dom_count: {}", dom_count);
}
Run Code Online (Sandbox Code Playgroud)
所以我添加了一个;并得到了这个:
.fold(0, |count, &(exists, _)| if exists {count + 1})
^^^^^^^^^^^^^^^^^^^^^ expected (), …Run Code Online (Sandbox Code Playgroud)