rust

Efficiently chunk large vector into a vector of vectors

我的梦境 提交于 2020-05-29 15:43:10
问题 I want to chunk a large vector into a vector of vectors. I know about chunks() , but am not sure of the best way to go from the iterator to a 2D Vec . I have found the following to work, but is there a better way to write this? let v: Vec<i32> = vec![1, 1, 1, 2, 2, 2, 3, 3, 3]; let v_chunked: Vec<Vec<i32>> = v.chunks(3).map(|x| x.to_vec()).collect(); println!("{:?}", v_chunked); // [[1, 1, 1], [2, 2, 2], [3, 3, 3]] https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist

The trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`

北慕城南 提交于 2020-05-29 09:43:24
问题 I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() .await?; println!("Status: {}", res.status()); let body = res.text().await?; println!("Body:\n\n{}", body); Ok(()) } Error that I'm getting: --> src/main.rs:6:15 | 6 | let res = reqwest::Client::new() | _______________^ 7 | | .get("https://hyper.rs") 8 | | .send() 9 | | .await?; | |__

The trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`

烈酒焚心 提交于 2020-05-29 09:41:58
问题 I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() .await?; println!("Status: {}", res.status()); let body = res.text().await?; println!("Body:\n\n{}", body); Ok(()) } Error that I'm getting: --> src/main.rs:6:15 | 6 | let res = reqwest::Client::new() | _______________^ 7 | | .get("https://hyper.rs") 8 | | .send() 9 | | .await?; | |__

The trait `std::future::Future` is not implemented for `std::result::Result<reqwest::Response, reqwest::Error>`

我怕爱的太早我们不能终老 提交于 2020-05-29 09:41:20
问题 I'm trying to run basic reqwest example: extern crate reqwest; extern crate tokio; #[tokio::main] async fn main() -> Result<(), reqwest::Error> { let res = reqwest::Client::new() .get("https://hyper.rs") .send() .await?; println!("Status: {}", res.status()); let body = res.text().await?; println!("Body:\n\n{}", body); Ok(()) } Error that I'm getting: --> src/main.rs:6:15 | 6 | let res = reqwest::Client::new() | _______________^ 7 | | .get("https://hyper.rs") 8 | | .send() 9 | | .await?; | |__

How to serialize http::HeaderMap into JSON?

寵の児 提交于 2020-05-29 06:42:46
问题 What is the proper way of serializing HTTP request headers (http::HeaderMap) into JSON in Rust? I am implementing an AWS Lambda function and I would like to have a simple echo function for debugging. use lambda_http::{lambda, IntoResponse, Request}; use lambda_runtime::{error::HandlerError, Context}; use log::{self, info}; use simple_logger; use std::error::Error; fn main() -> Result<(), Box<dyn Error>> { simple_logger::init_with_level(log::Level::Debug)?; info!("Starting up..."); lambda!

How to serialize http::HeaderMap into JSON?

柔情痞子 提交于 2020-05-29 06:41:33
问题 What is the proper way of serializing HTTP request headers (http::HeaderMap) into JSON in Rust? I am implementing an AWS Lambda function and I would like to have a simple echo function for debugging. use lambda_http::{lambda, IntoResponse, Request}; use lambda_runtime::{error::HandlerError, Context}; use log::{self, info}; use simple_logger; use std::error::Error; fn main() -> Result<(), Box<dyn Error>> { simple_logger::init_with_level(log::Level::Debug)?; info!("Starting up..."); lambda!

Reimplementation of LinkedList: IterMut not compiling

倖福魔咒の 提交于 2020-05-29 06:24:13
问题 I am trying to reimplement LinkedList from scratch and I am having some trouble with the IterMut part. I just won't compile and I can't figure out why. The compiler just gives me the message: src/lib.rs:59:19: 59:27 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495] src/lib.rs:59 self.next.as_mut().map(|head| { ^~~~~~~~ src/lib.rs:58:5: 63:6 help: consider using an explicit lifetime parameter as shown: fn next(&'a mut self) -> Option<&'a mut A> src

What happens when I call std::mem::drop with a reference instead of an owned value?

心已入冬 提交于 2020-05-29 06:14:40
问题 fn main() { let k = "fire"; drop(k); println!("{:?}", k); } Playground Why am I still able to use k after dropping it? Does drop not deref a reference automatically? If yes, then why? What does the implementation of Drop look like for &str ? 回答1: What happens when I call std::mem::drop with a reference The reference itself is dropped. a reference instead of an owned value A reference is a value. Why am I still able to use k after dropping it? Because immutable pointers implement Copy . You

Whats the best way to join many vectors into a new vector?

早过忘川 提交于 2020-05-29 04:56:51
问题 To create a new vector with the contents of other vectors, I'm currently doing this: fn func(a: &Vec<i32>, b: &Vec<i32>, c: &Vec<i32>) { let abc = Vec<i32> = { let mut tmp = Vec::with_capacity(a.len(), b.len(), c.len()); tmp.extend(a); tmp.extend(b); tmp.extend(c); tmp }; // ... } Is there a more straightforward / elegant way to do this? 回答1: There is a concat method that can be used for this, however the values need to be slices, or borrowable to slices, not &Vec<_> as given in the question.

Can I create private enum constructors?

非 Y 不嫁゛ 提交于 2020-05-29 04:28:04
问题 In Haskell I could do something like this (example adapted from Learn You A Haskell) module Shapes ( Shape, newCircle, newRectangle, ... -- other functions for manipulating the shapes ) data Shape = Circle Int Int Float -- x, y, radius | Rectangle Int Int Int Int -- x1, y1, x2, y2 newCircle :: Float -> Shape newCircle r = Circle 0 0 r newRectangle :: Int -> Int -> Shape newRectangle w h = Rectangle 0 0 w h ... -- other functions for manipulating the shapes That would allow me to only expose