为什么在元组上使用Some将类型包装在&[]中?

时间:2019-03-23 20:05:17

标签: twitter rust

我正在使用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))>`

1 个答案:

答案 0 :(得分:1)

您正在反向阅读错误消息。清理了一下,上面写着:

  

约束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));