我正在使用twitter_stream
箱从Twitter中提取数据。API 支持按某个参数过滤数据;就我而言,我正在尝试使用边界框位置进行过滤。该库采用Option<((f64, f64), (f64, f64))>
,因此我以这种形式创建了一个元组:
let bounds = ((0.59 as f64, 0.59 as f64), (0.59 as f64, 0.59 as f64));
当我Some(bounds)
将其包装在Option
中时,我似乎
Option<&[((f64, f64), (f64, f64))]>
这在我的元组周围增加了一个&[]
,我不明白这意味着什么或为什么它在那里。我最好的猜测是,这意味着现在有一个借来的数组,元组周围有一个元素,但我不明白为什么会有一个数组,我尝试在任何地方添加.to_owned()
,它没有改变任何东西,所以我觉得我离基地太远了。
法典:
extern crate twitter_stream;
use twitter_stream::rt::{self, Future, Stream};
use twitter_stream::{Token, TwitterStreamBuilder};
fn main() {
let bounds = ((0.59 as f64, 0.59 as f64), (0.59 as f64, 0.59 as f64));
let future = TwitterStreamBuilder::filter(Token::new(
"consumer_key",
"consumer_secret",
"access_token",
"access_secret",
))
.locations(Some(bounds))
.listen()
.unwrap()
.flatten_stream()
.for_each(|json| {
println!("{}", json);
Ok(())
})
.map_err(|e| println!("error: {}", e));
rt::run(future);
}
错误:
error[E0277]: the trait bound `std::option::Option<&[((f64, f64), (f64, f64))]>: std::convert::From<std::option::Option<((f64, f64), (f64, f64))>>` is not satisfied
--> src/main.rs:9:14
|
9 | .locations(Some(bounds))
| ^^^^^^^^^ the trait `std::convert::From<std::option::Option<((f64, f64), (f64, f64))>>` is not implemented for `std::option::Option<&[((f64, f64), (f64, f64))]>`
|
= help: the following implementations were found:
<std::option::Option<&'a T> as std::convert::From<&'a std::option::Option<T>>>
<std::option::Option<&'a mut T> as std::convert::From<&'a mut std::option::Option<T>>>
<std::option::Option<T> as std::convert::From<T>>
= note: required because of the requirements on the impl of `std::convert::Into<std::option::Option<&[((f64, f64), (f64, f64))]>>` for `std::option::Option<((f64, f64), (f64, f64))>`
您正在向后阅读错误消息。清理了一下,上面写着:
绑定
Option<&[BoundingBox]>: From<Option<BoundingBox>>
的特质不满足
也就是说,无法从BoundingBox
创建&[BoundingBox]
。
locations
方法定义为:
pub fn locations(
&mut self,
locations: impl Into<Option<&'a [BoundingBox]>>
) -> &mut Self
也就是说,它采用任何可以转换为坐标框切片Option
的类型。您正在尝试仅提供单个坐标框的Option
。
相反,创建一个值的数组并创建一个切片:
.locations(Some(&[bounds][..]))
或
.locations(Some(std::slice::from_ref(&bounds)))
您还可以利用Option<T>
实现From<T>
的事实:
.locations(&[bounds][..])
或
.locations(std::slice::from_ref(&bounds))
另请参阅:
- Rust 中的默认函数参数
为了可读性,我假装此类型别名存在:
type BoundingBox = ((f64, f64), (f64, f64));