希望你能早点学到的 Rust 技巧
- 双向引用
- 实现 Deref
- 小心实现 Deref
- 内部可变性的使用
- 接受 unsafe
- ...
ReadMore: https://rauljordan.com/rust-concepts-i-wish-i-learned-earlier/
tantivy - Rust 编写的全文搜索引擎库
这个项目允许按每个线程和每个回溯实时内存剖析。
tantivy更接近Apache Lucene,而不是Elasticsearch或Apache Solr。他不是一个现成的搜索引擎服务器,而是一个 crates 。
Tantivy的灵感来自Lucene的设计。
ReadMore:https://github.com/quickwit-oss/tantivy
Type-erasing trait parameters in Rust
- Rust 中的基本类型擦除
- 使用类型参数进行类型擦除
- 擦除所有类型参数
- ...
例子
代码语言:javascript复制#[tokio::main]
async fn main() {
let mut service_map = ServiceMap::default();
service_map.register(Foo {});
service_map.register(Bar {});
// prints: "got i64: 123"
service_map.call(123i64).await;
// prints: Point(123.0, 456.0)
service_map.call((123.0f64, 456.0f64)).await;
// prints nothing, since no service has been registered for `Request = &str`
service_map.call("str").await;
}
struct Foo {}
impl Service<i64> for Foo {
type Response = String;
type Future = Ready<String>;
fn call(&mut self, req: i64) -> Ready<String> {
ready(format!("got i64: {req:?}"))
}
}
#[derive(Debug)]
struct Point(f64, f64);
struct Bar {}
impl Service<(f64, f64)> for Bar {
type Response = Point;
type Future = Ready<Point>;
fn call(&mut self, req: (f64, f64)) -> Ready<Point> {
ready(Point(req.0, req.1))
}
}
ReadMore:https://fredrik.space/posts/rust-erased-trait-parameters