Why using a separate variable breaks Rust's rules?

72 Views Asked by At

I am trying to subscribe to Binance BookTicker streams. Everything was ok til I tried to pass a dynamic list to the subscriber method.

Working version:

use binance_spot_connector_rust::{
    market_stream::book_ticker::BookTickerStream,
    tungstenite::BinanceWebSocketClient,
};

let stream_adr = "wss://stream.binance.com:9443/ws";
let mut conn = BinanceWebSocketClient::connect_with_url(stream_adr)?;

conn.subscribe(vec![
    &BookTickerStream::from_symbol("BTCUSDT").into(),
    &BookTickerStream::from_symbol("BNBBUSD").into(),
]);

The version I tried:

let symbol_lst = vec!["BTCUSDT".to_string(), "BNBBUSD".to_string(), "BTCUSDT".to_string()];

let lst = symbol_lst.iter()
    .map(|x| BookTickerStream::from_symbol(&x))
    .collect::<Vec<_>>()
    ;

// try 1
conn.subscribe(lst.into_iter().map(|x| x.into()));
// ^^^^ the trait `From<BookTickerStream>` is not implemented for `&binance_spot_connector_rust::websocket::Stream`

// try 2
conn.subscribe(lst.into_iter().map(|x| &x.into()));
// returns a reference to data owned by the current function

The subscribe function accepts an iterator of Stream, which means the BookTickerStream struct should be converted into.

// the crate's code from its repository
pub fn subscribe<'a>(&mut self, streams: impl IntoIterator<Item = &'a Stream>) -> u64 {
   ...
}

The crate::websocket::Stream is private and cannot be created manually, so the conversion is required.

ENV

Rust: 1.74 - stable
Crate: binance_spot_connector_rust = { version = "1", features = ["full"] }
0

There are 0 best solutions below