rust

How to recursively take the last argument of a macro?

泄露秘密 提交于 2020-01-06 16:25:07
问题 Using a simple recursive macro like the example below, its common to take the first argument, then glob the rest. macro_rules! count_tts { () => {0usize}; ($_head:tt $($tail:tt)*) => {1usize + count_tts!($($tail)*)}; } Is there a way to recursively take the last argument? This makes it possible to: Handle the arguments in reverse. Take all the previous arguments into account (count them for example, see related question) Something like ($($head:tt)* $tail:tt) ... but this doesn't work. 回答1:

Enumerating generic structs

这一生的挚爱 提交于 2020-01-06 15:16:22
问题 I wanted to try to build a proper implementation of Peano numbers using struct s, but it seems my generics game is not good enough yet and I could use some help. I read the docs on generics and some StackOverflow questions, but they don't fit my case. I introduced a Peano trait and Zero and Succ types: trait Peano {} struct Zero; struct Succ<T: Peano>(T); And implemented a Peano trait for both types to be able to abstract over both: impl Peano for Zero {} impl<T> Peano for Succ<T> where T:

How to return an instance of a trait?

二次信任 提交于 2020-01-06 15:09:50
问题 I'm working on a SQL migration tool. Right now I only support Postgresql but i'd like to add Mysql, etc. I have the following trait that drivers need to implement: pub trait Driver { fn ensure_migration_table_exists(&self); fn remove_migration_table(&self); fn get_current_number(&self) -> i32; fn set_current_number(&self, number: i32); fn migrate(&self, migration: String, number: i32) -> MigrateResult<()>; } I want to make a function get_driver which would have the following conceptual

Crate cannot find path

↘锁芯ラ 提交于 2020-01-06 12:17:40
问题 I am trying to use this crate to generate an ethereum address: https://docs.rs/ethkey/0.2.5/ethkey/ use ethkey::prelude::*; fn main() { let key = EthAccount::load_or_generate("~/", "passwd") .expect("should load or generate new eth key"); println!("{:?}", key.address()) } This is the example from the documentation and it doesnt seem to work. I get the error below: cargo run Compiling ethkey v0.1.0 (/Users/samueldare/Documents/Code/Thor/ethkey) Finished dev [unoptimized + debuginfo] target(s)

Crate cannot find path

一笑奈何 提交于 2020-01-06 12:17:14
问题 I am trying to use this crate to generate an ethereum address: https://docs.rs/ethkey/0.2.5/ethkey/ use ethkey::prelude::*; fn main() { let key = EthAccount::load_or_generate("~/", "passwd") .expect("should load or generate new eth key"); println!("{:?}", key.address()) } This is the example from the documentation and it doesnt seem to work. I get the error below: cargo run Compiling ethkey v0.1.0 (/Users/samueldare/Documents/Code/Thor/ethkey) Finished dev [unoptimized + debuginfo] target(s)

Crate cannot find path

橙三吉。 提交于 2020-01-06 12:17:04
问题 I am trying to use this crate to generate an ethereum address: https://docs.rs/ethkey/0.2.5/ethkey/ use ethkey::prelude::*; fn main() { let key = EthAccount::load_or_generate("~/", "passwd") .expect("should load or generate new eth key"); println!("{:?}", key.address()) } This is the example from the documentation and it doesnt seem to work. I get the error below: cargo run Compiling ethkey v0.1.0 (/Users/samueldare/Documents/Code/Thor/ethkey) Finished dev [unoptimized + debuginfo] target(s)

How to get the cookie from a GET response?

冷暖自知 提交于 2020-01-06 07:58:26
问题 I am writing a function that makes a GET request to a website and returns the response cookie: extern crate futures; extern crate hyper; extern crate tokio_core; use tokio_core::reactor::Core; use hyper::Client; use std::error::Error; use hyper::header::Cookie; use futures::future::Future; fn get_new_cookie() -> Result<String, Box<Error>> { println!("Getting cookie..."); let core = Core::new()?; let client = Client::new(&core.handle()); println!("Created client"); let uri = "http://www.cnn

What's the difference between len() and capacity()?

不问归期 提交于 2020-01-06 07:47:11
问题 When I create a vector, the length and the capacity are the same. What is the difference between these methods? fn main() { let vec = vec![1, 2, 3, 4, 5]; println!("Length: {}", vec.len()); // Length: 5 println!("Capacity: {}", vec.capacity()); // Capacity: 5 } 回答1: Growable vectors reserve space for future additions, hence the difference between allocated space ( capacity ) and actually used space ( length ). This is explained in the standard library's documentation for Vec: The capacity of

How can I confirm that Eclipse content assist works in Corrosion?

自古美人都是妖i 提交于 2020-01-06 07:13:50
问题 I thought I'd pick up Rust, so I installed it alongside Corrosion for Eclipse. Corrosion's description reads: Corrosion provides a rich and smart Rust editor with: - Syntax highlighting (using TextMate grammar) and Error reporting, Hover. Content assist . Jump to references, Code Outline, Formatting... provided by the Rust Language Server I've made sure to install RLS, and autocomplete seems to work without any problems in IntelliJ IDEA so I assume the install was successful. The RLS setting

How can I confirm that Eclipse content assist works in Corrosion?

佐手、 提交于 2020-01-06 07:13:25
问题 I thought I'd pick up Rust, so I installed it alongside Corrosion for Eclipse. Corrosion's description reads: Corrosion provides a rich and smart Rust editor with: - Syntax highlighting (using TextMate grammar) and Error reporting, Hover. Content assist . Jump to references, Code Outline, Formatting... provided by the Rust Language Server I've made sure to install RLS, and autocomplete seems to work without any problems in IntelliJ IDEA so I assume the install was successful. The RLS setting