打算照着go的http api实现一个rust的web框架,目前写了点基础的代码见https://github.com/xcaptain/voyager.rs,但是遇到了问题导致路由部分写不下去了,rust对闭包的类型校验也是这么严格,错误信息如下,提示说考虑使用boxed closure或trait object但是搜了一下没找到类似的资料自己也还没想好怎么改代码,求懂行的大神赐教下
joey@voyager-pc ~/P/voyager> cargo run --example hello
Compiling voyager v0.1.0 (/home/joey/Projects/voyager)
error[E0308]: mismatched types
--> examples/hello.rs:20:49
|
20 | m.handle("/world".to_string(), Handler::new(world_handler));
| ^^^^^^^^^^^^^ expected closure, found a different closure
|
= note: expected type `[closure@examples/hello.rs:10:25: 13:6]`
found type `[closure@examples/hello.rs:14:25: 17:6]`
= note: no two closures, even if identical, have the same type
= help: consider boxing your closure and/or using it as a trait object
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: Could not compile `voyager`.
To learn more, run the command again with --verbose.
1
共 4 条评论, 1 页
评论区
写评论或许可以参考别人如何实现,actix web。
目前实现了一个简单的http框架,api已经很接近go的
net/http
了,这几天我再优化下看看能不能做一些更复杂的事情感谢,我在官方论坛也问了这个问题,https://users.rust-lang.org/t/how-to-use-boxed-closure-or-trait-object/25786/5 现在确实已经修复了,按照回我的那几个人的说法如果要用trait的话就要加上dyn来表明可以接受任何实现了这个trait的结构,如果要用closure的话就要加上box这样闭包在编译时能知道大小。
用trait太麻烦相关的结构都要加上trait bound,所以改用了boxed closure.
看样子已经修好了。
具体使用可以看它的教程:https://doc.rust-lang.org/book/ch19-05-advanced-functions-and-closures.html