rust

Return Future value from a function

北城以北 提交于 2021-02-05 10:30:38
问题 I recently started to learn Rust and I'm not sure how I can return future value from a function that should return a Result. When I try to return just the response variable and remove the Result output, I get an error: cannot use the ? operator in a function that returns std::string::String #[tokio::main] async fn download() -> Result<(),reqwest::Error> { let url = "https://query1.finance.yahoo.com/v8/finance/chart/TSLA"; let response = reqwest::get(url) .await? .text() .await?; Ok(response)

Return Future value from a function

帅比萌擦擦* 提交于 2021-02-05 10:25:10
问题 I recently started to learn Rust and I'm not sure how I can return future value from a function that should return a Result. When I try to return just the response variable and remove the Result output, I get an error: cannot use the ? operator in a function that returns std::string::String #[tokio::main] async fn download() -> Result<(),reqwest::Error> { let url = "https://query1.finance.yahoo.com/v8/finance/chart/TSLA"; let response = reqwest::get(url) .await? .text() .await?; Ok(response)

fancy-regex crate multiple matches

点点圈 提交于 2021-02-05 09:26:17
问题 I'm using the fancy-regex crate since I need lookaheads in my Regex but it seems like it's not getting all of the matches in a string like I can with the regex crate which fancy-regex is built upon: What I'm trying: use fancy_regex::Regex; let value = "Rect2(Vector2(0, 0), Vector2(0, 0))"; let re = Regex::new(r"\(([^()]*)\)").expect("Unable to create regex for values in parenthesis"); let results = re.captures(value).expect("Error running regex").expect("No matches found"); // Since 0 gets

Can you specify a non-static lifetime for threads? [duplicate]

心已入冬 提交于 2021-02-05 09:14:26
问题 This question already has answers here : How can I pass a reference to a stack variable to a thread? (1 answer) Thread references require static lifetime? (1 answer) How do I use static lifetimes with threads? (2 answers) How can I send non-static data to a thread in Rust and is it needed in this example? (1 answer) Closed 1 year ago . Here's a toy example of my problem: use std::sync::{Arc, Mutex}; fn operate_in_chunks(vec: &mut Vec<f32>) { let chunk_size = 10; let mutex_vec: Arc<Mutex<&mut

Can you specify a non-static lifetime for threads? [duplicate]

天大地大妈咪最大 提交于 2021-02-05 09:14:07
问题 This question already has answers here : How can I pass a reference to a stack variable to a thread? (1 answer) Thread references require static lifetime? (1 answer) How do I use static lifetimes with threads? (2 answers) How can I send non-static data to a thread in Rust and is it needed in this example? (1 answer) Closed 1 year ago . Here's a toy example of my problem: use std::sync::{Arc, Mutex}; fn operate_in_chunks(vec: &mut Vec<f32>) { let chunk_size = 10; let mutex_vec: Arc<Mutex<&mut

Why does a trait type `Box<dyn Error>` error with “Sized is not implemented” but `async fn() -> Result<(), Box<dyn Error>>` works?

浪子不回头ぞ 提交于 2021-02-05 08:23:30
问题 I’ve the following simplified code. use async_trait::async_trait; // 0.1.36 use std::error::Error; #[async_trait] trait Metric: Send { type Output; type Error: Error; async fn refresh_metric(&mut self) -> Result<Self::Output, Self::Error>; } #[derive(Default)] struct StaticMetric; #[async_trait] impl Metric for StaticMetric { type Output = (); type Error = Box<dyn Error>; async fn refresh_metric(&mut self) -> Result<Self::Output, Self::Error> { Ok(()) } } struct LocalSystemData<T> { inner: T,

Why does a trait type `Box<dyn Error>` error with “Sized is not implemented” but `async fn() -> Result<(), Box<dyn Error>>` works?

a 夏天 提交于 2021-02-05 08:23:29
问题 I’ve the following simplified code. use async_trait::async_trait; // 0.1.36 use std::error::Error; #[async_trait] trait Metric: Send { type Output; type Error: Error; async fn refresh_metric(&mut self) -> Result<Self::Output, Self::Error>; } #[derive(Default)] struct StaticMetric; #[async_trait] impl Metric for StaticMetric { type Output = (); type Error = Box<dyn Error>; async fn refresh_metric(&mut self) -> Result<Self::Output, Self::Error> { Ok(()) } } struct LocalSystemData<T> { inner: T,

Getting first member of a BTreeSet

萝らか妹 提交于 2021-02-05 07:31:18
问题 In Rust, I have a BTreeSet that I'm using to keep my values in order. I have a loop that should retrieve and remove the first (lowest) member of the set. I'm using a cloned iterator to retrieve the first member. Here's the code: use std::collections::BTreeSet; fn main() { let mut start_nodes = BTreeSet::new(); // add items to the set while !start_nodes.is_empty() { let mut start_iter = start_nodes.iter(); let mut start_iter_cloned = start_iter.cloned(); let n = start_iter_cloned.next().unwrap

How to coerce a Vec of structs to a Vec of trait objects?

五迷三道 提交于 2021-02-05 07:21:29
问题 Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box<dyn Model> . use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4 } } impl Model for Message { fn id(&self) -> i32 { 3 } } struct DB { users: Vec<Box<User>>, messages: Vec<Box<Message>>, tables: HashMap<String, Vec<Box<dyn Model>>>, } impl DB { fn new() -> Self { let users: Vec<Box<User>> = Vec::new(); let messages: Vec<Box

How to coerce a Vec of structs to a Vec of trait objects?

元气小坏坏 提交于 2021-02-05 07:21:06
问题 Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box<dyn Model> . use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4 } } impl Model for Message { fn id(&self) -> i32 { 3 } } struct DB { users: Vec<Box<User>>, messages: Vec<Box<Message>>, tables: HashMap<String, Vec<Box<dyn Model>>>, } impl DB { fn new() -> Self { let users: Vec<Box<User>> = Vec::new(); let messages: Vec<Box