如果我想解压缩一个元组并将其作为参数传递,有一种方法可以做到这一点:
//Does not compile
fn main() {
let tuple = (10, Vec::new());
foo(tuple);
}
fn foo(a: i32, b: Vec<i32>) {
//Does stuff.
}
Run Code Online (Sandbox Code Playgroud)
而不是必须这样做:
fn main() {
let tuple = (10, Vec::new());
foo(tuple.0, tuple.1);
}
fn foo(a: i32, b: Vec<i32>) {
//Does stuff.
}
Run Code Online (Sandbox Code Playgroud) 这工作,因为Iterator
工具rev()
,其中self
是DoubleEndedIterator
:
let vec: Vec<i32> = Vec::new();
for x in vec.iter().rev() {
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我改变vec.iter().rev()
到&vec.rev()
它不会因为编译:
Run Code Online (Sandbox Code Playgroud)no method named `rev` found for type `std::vec::Vec<i32>` in the current scope
此外:
Run Code Online (Sandbox Code Playgroud)the method `rev` exists but the following trait bounds were not satisfied: `std::vec::Vec<i32> : std::iter::Iterator`, `[i32] : std::iter::Iterator`
但是for循环不会隐式调用IntoIterator
吗?是&vec
或被vec.iter()
认为是惯用的Rust?
给定一个具有泛型的结构,Option<T>
其中T
可能无法实现Clone
为什么不能None
被克隆?aNone
的类型T
和其他的不一样None
吗?例如:
struct Foo<T> {
bar: Vec<Option<T>>,
}
impl <T> Foo<T> {
fn blank(size: usize) -> Foo<T> {
Foo {
bar: vec![None; size],
}
}
}
Run Code Online (Sandbox Code Playgroud) 使用多个键排序时,如何反转单个键的顺序?例如:
vec.sort_by_key(|k| (foo(k).reverse(), bar(k)));
Run Code Online (Sandbox Code Playgroud) 当使用newtype模式时,我经常会得到冗长的信息:
extern crate derive_more;
use derive_more::*;
#[derive(Add, Sub, Mul, Div, ..., Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
struct Foo(i32);
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以将其简化为以下形式:
#[derive(Num)]
struct Foo(i32);
Run Code Online (Sandbox Code Playgroud)
Num
派生宏在哪里?
我发现了这一点,但似乎无法扩展属性中的宏。 此答案讨论了必须如何将属性附加到项目,排除了这一点:
#[proc_macro_derive(Num)]
pub fn num_derive(_: TokenStream) -> TokenStream {
let gen = quote! {
#[derive(Add, Sub, Mul, Div, ..., Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
};
gen.into()
}
Run Code Online (Sandbox Code Playgroud) 将两个元素从同一个向量传递给函数时,借用检查器将不允许其中一个元素是可变的.
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut vec: Vec<Point> = Vec::new();
foo(&mut vec[0], &vec[1]);
}
fn foo(pnt_1: &mut Point, pnt_2: &Point) {
}
Run Code Online (Sandbox Code Playgroud)
错误:不能借用
vec
为不可变的,因为它也被借用为可变的
vec
从来没有借过foo
,vec[0]
是借来的,vec[0]
是一个Point
.
如何将同一个集合中的多个元素传递给一个或多个元素可变的函数?
如何为 z3 优化器设置超时,以便在超时时为您提供最知名的解决方案?
from z3 import *
s = Optimize()
# Hard Problem
print(s.check())
print(s.model())
Run Code Online (Sandbox Code Playgroud)
后续问题,你可以将z3设置为随机爬山还是始终执行完整搜索?
我有包含变量的枚举:
enum Asymmetric {
One(i32),
Two(i32, i32),
}
Run Code Online (Sandbox Code Playgroud)
我想只更改已存在的枚举的一个字段,而不重新分配整个枚举.我的代码(游乐场):
// Does not compile
fn main() {
let two = Asymmetric::Two(4, 5);
let mut vec = vec![two];
foo(&mut vec[0]);
}
fn foo(baa: &mut Asymmetric) {
match baa {
&mut Asymmetric::Two(x0, x1) => {
x0 = 6;
}
_ => {}
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致此错误:
error[E0384]: re-assignment of immutable variable `x0`
--> src/main.rs:16:13
|
15 | &mut Asymmetric::Two(x0, x1) => {
| -- first assignment to `x0`
16 | x0 = …
Run Code Online (Sandbox Code Playgroud) 我正在根据两个标准对向量进行排序。第一个是浮点数NaN
,第二个是字符串,用于按字典顺序断开关系。
vec.sort_by(|a, b| {
match (foo(a) as f64 / bar(a) as f64).partial_cmp(&(foo(b) as f64 / bar(b) as f64)) {
Some(x) => {
Ordering::Equal => name(a).cmp(name(b)),
other => other,
}
None() => {
//Not sure what to put here.
}
}
}
Run Code Online (Sandbox Code Playgroud)
foo(a)
返回 int > 0,
bar(a)
返回 int >= 0,
name(a)
返回& String
.
如何排序NaN
,使其大于任何其他数字,并等于任何其他数字NaN
(字典顺序决胜局)?
我有一个结构,除其他数据外,还有一个唯一的id
:
struct Foo {
id: u32,
other_data: u32,
}
Run Code Online (Sandbox Code Playgroud)
我想使用id
键作为键并将其保留在结构中:
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
impl PartialEq for Foo {
fn eq(&self, other: &Foo) -> bool {
self.id == other.id
}
}
impl Eq for Foo {}
impl Hash for Foo {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
Run Code Online (Sandbox Code Playgroud)
这有效:
pub fn bar() {
let mut baz: HashSet<Foo> = HashSet::new();
baz.insert(Foo {
id: 1,
other_data: 2,
});
let other_data = baz.get(&Foo {
id: …
Run Code Online (Sandbox Code Playgroud)