我正在使用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))
也可以看看:
为了便于阅读,我假装存在这种类型的别名:
type BoundingBox = ((f64, f64), (f64, f64));