我有一个通用函数,打印至少两个项目:
use std::fmt::Display;
fn print_min<T: PartialOrd + Display>(a: &T, b: &T) {
println!("min = {}", if a < b { a } else { b });
}
Run Code Online (Sandbox Code Playgroud)
这适用于实现PartialOrd
和Display
特征的任何东西:
print_min(&45, &46);
// min = 45
print_min(&"a", &"b");
// min = a
Run Code Online (Sandbox Code Playgroud)
必须放入PartialOrd + Display
函数定义是一种丑陋的,特别是如果我想拥有一大堆对此进行操作的函数(例如,实现二进制搜索树),或者我的边界变得更复杂.我的第一个倾向是尝试编写一个类型别名:
type PartialDisplay = PartialOrd + Display;
Run Code Online (Sandbox Code Playgroud)
但这给了我一些相当奇怪的编译器错误:
error[E0393]: the type parameter `Rhs` must be explicitly specified
--> src/main.rs:7:23
|
7 | type PartialDisplay = PartialOrd + Display;
| ^^^^^^^^^^ missing reference …
Run Code Online (Sandbox Code Playgroud) 这有效:
struct Foo<T, F>
where
F: Fn() -> Option<T>,
{
f: F,
}
Run Code Online (Sandbox Code Playgroud)
但这给了我编译错误:
struct Bar<I, T, F>
where
F: Fn(I) -> Option<T>,
{
f: F,
}
Run Code Online (Sandbox Code Playgroud)
error[E0392]: parameter `I` is never used
--> src/main.rs:1:12
|
1 | struct Bar<I, T, F>
| ^ unused type parameter
|
= help: consider removing `I` or using a marker such as `std::marker::PhantomData`
error[E0392]: parameter `T` is never used
--> src/main.rs:1:15
|
1 | struct Bar<I, T, F>
| ^ unused type parameter …
Run Code Online (Sandbox Code Playgroud) 碰到一个问题我还没找到答案.
使用Scala在playframework 2上运行.
需要编写一个执行多个Future调用的Action方法.我的问题:1)附加的代码是否是非阻塞的,因此看起来应该如何?2)是否保证在任何给定时间都捕获两个DAO结果?
def index = Action.async {
val t2:Future[Tuple2[List[PlayerCol],List[CreatureCol]]] = for {
p <- PlayerDAO.findAll()
c <- CreatureDAO.findAlive()
}yield(p,c)
t2.map(t => Ok(views.html.index(t._1, t._2)))
}
Run Code Online (Sandbox Code Playgroud)
感谢您的反馈意见.