rust

Is there any way to unpack an iterator into a tuple?

落爺英雄遲暮 提交于 2020-06-21 11:34:07
问题 Is there any way to accomplish something like the following: let v = vec![1, 2, 3]; let (a, b) = v.iter().take(2); Such that a = 1 and b = 2 at the end? I know I could just use a vector but I would like to have named variables. 回答1: This may not be exactly what you asked for, but I suppose you rarely want to convert an arbitrarily large vector to a tuple anyway. If you just want to extract the first few elements of a vector into a tuple, you can do so using slice pattern matching : fn main()

Why is iterating over a collection via `for` loop considered a “move” in Rust?

∥☆過路亽.° 提交于 2020-06-21 05:35:51
问题 I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `v` --> src\main.rs:7:22 | 2 | let v = vec![100, 32, 57]; | - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait 3 | for i in v { | - | | | value moved here | help: consider borrowing to avoid moving into the for loop: `&v` ... 7 | println!("{:?}", v); | ^ value

Why is iterating over a collection via `for` loop considered a “move” in Rust?

家住魔仙堡 提交于 2020-06-21 05:34:58
问题 I have the below Rust program. fn main() { let v = vec![100, 32, 57]; for i in v { println!("{}", i); } println!("{:?}", v); } When I run it, I get: error[E0382]: borrow of moved value: `v` --> src\main.rs:7:22 | 2 | let v = vec![100, 32, 57]; | - move occurs because `v` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait 3 | for i in v { | - | | | value moved here | help: consider borrowing to avoid moving into the for loop: `&v` ... 7 | println!("{:?}", v); | ^ value

rust pass box reference without move

雨燕双飞 提交于 2020-06-17 13:14:11
问题 Background: I'm writing a RDBMS in rust The db.catalog maintain a hashmap from table_id to table: pub struct Catalog { table_id_table_map: HashMap<i32, Box<dyn Table>>, } And when I add a boxed table to the catalog, move occurred. Then I can't use table instance anymore: // create table let table = create_random_heap_table(....); // add to catalog db.get_catalog().add_table(Box::new(table), "heap table", ""); // access table instance let table_id = table.get_id(); compile error: error[E0382]:

How do I use log4rs' RollingFileAppender to incorporate rolling logging?

泪湿孤枕 提交于 2020-06-17 07:04:24
问题 I am trying to build a logger based on a rolling policy. Below is the closest I was able to implement: let logfile = FileAppender::builder() .encoder(Box::new(PatternEncoder::new("{d} {l}::{m}{n}"))) .build("log/output.log")?; let config = Config::builder() .appender(Appender::builder().build("logfile", Box::new(logfile))) .build(Root::builder() .appender("logfile") .build(LevelFilter::Debug))?; log4rs::init_config(config)?; This helped me log messages at all levels. However, logging to a

How do I use log4rs' RollingFileAppender to incorporate rolling logging?

最后都变了- 提交于 2020-06-17 07:04:04
问题 I am trying to build a logger based on a rolling policy. Below is the closest I was able to implement: let logfile = FileAppender::builder() .encoder(Box::new(PatternEncoder::new("{d} {l}::{m}{n}"))) .build("log/output.log")?; let config = Config::builder() .appender(Appender::builder().build("logfile", Box::new(logfile))) .build(Root::builder() .appender("logfile") .build(LevelFilter::Debug))?; log4rs::init_config(config)?; This helped me log messages at all levels. However, logging to a

Trouble with Rust Lifetime in Generic function [duplicate]

a 夏天 提交于 2020-06-16 11:09:50
问题 This question already has answers here : Lifetime error when creating a function that returns a value implementing serde::Deserialize (2 answers) Closed 4 months ago . I have a simple function that I want to make generic, in rust. I am getting a lifetime error. I am still getting the hang of the lifetime side of rust. The function simply converts 1 struct into another using serde's serialization. Here is a a rust playground with the full simple scenario. Code: pub fn convert<'de: 'a, 'a, T>

Trouble with Rust Lifetime in Generic function [duplicate]

拥有回忆 提交于 2020-06-16 11:09:10
问题 This question already has answers here : Lifetime error when creating a function that returns a value implementing serde::Deserialize (2 answers) Closed 4 months ago . I have a simple function that I want to make generic, in rust. I am getting a lifetime error. I am still getting the hang of the lifetime side of rust. The function simply converts 1 struct into another using serde's serialization. Here is a a rust playground with the full simple scenario. Code: pub fn convert<'de: 'a, 'a, T>

Trouble with Rust Lifetime in Generic function [duplicate]

人走茶凉 提交于 2020-06-16 11:06:37
问题 This question already has answers here : Lifetime error when creating a function that returns a value implementing serde::Deserialize (2 answers) Closed 4 months ago . I have a simple function that I want to make generic, in rust. I am getting a lifetime error. I am still getting the hang of the lifetime side of rust. The function simply converts 1 struct into another using serde's serialization. Here is a a rust playground with the full simple scenario. Code: pub fn convert<'de: 'a, 'a, T>

How does String::from(“”) & “”.to_string() differ in Rust? [duplicate]

夙愿已清 提交于 2020-06-16 09:42:30
问题 This question already has answers here : How to create a String directly? (3 answers) What is the difference between these 3 ways of declaring a string in Rust? (1 answer) Closed last month . How does String::from("") & "".to_string() differ in Rust? Is there any difference in stack and heap allocation in both cases? 回答1: How does String::from("") & "".to_string() differ in Rust? They're part of different protocols (traits): std::convert::From and alloc::string::ToString[0]. However, when it