rust

Reverse specific key when sorting with multiple keys

核能气质少年 提交于 2020-02-23 10:25:09
问题 When sorting with multiple keys, how can I reverse the order of an individual key? For example: vec.sort_by_key(|k| (foo(k).reverse(), bar(k))); 回答1: You can use sort_by paired with Ordering::reverse instead of sort_by_key . use std::cmp::Ordering; #[derive(Debug)] struct Foo(&'static str, u8); impl Foo { fn name(&self) -> &str { self.0 } fn len(&self) -> u8 { self.1 } } fn main() { let mut vec = vec![Foo("alpha", 1), Foo("beta", 2), Foo("beta", 1)]; vec.sort_by(|a, b| { match a.name().cmp(b

How do I implement the Chain of Responsibility pattern using a chain of trait objects?

Deadly 提交于 2020-02-23 09:50:50
问题 I'm trying to implement the Chain of Responsibility design pattern in Rust: pub trait Policeman<'a> { fn set_next(&'a mut self, next: &'a Policeman<'a>); } pub struct Officer<'a> { deduction: u8, next: Option<&'a Policeman<'a>>, } impl<'a> Officer<'a> { pub fn new(deduction: u8) -> Officer<'a> { Officer {deduction, next: None} } } impl<'a> Policeman<'a> for Officer<'a> { fn set_next(&'a mut self, next: &'a Policeman<'a>) { self.next = Some(next); } } fn main() { let vincent = Officer::new(8);

How do I implement the Chain of Responsibility pattern using a chain of trait objects?

て烟熏妆下的殇ゞ 提交于 2020-02-23 09:50:38
问题 I'm trying to implement the Chain of Responsibility design pattern in Rust: pub trait Policeman<'a> { fn set_next(&'a mut self, next: &'a Policeman<'a>); } pub struct Officer<'a> { deduction: u8, next: Option<&'a Policeman<'a>>, } impl<'a> Officer<'a> { pub fn new(deduction: u8) -> Officer<'a> { Officer {deduction, next: None} } } impl<'a> Policeman<'a> for Officer<'a> { fn set_next(&'a mut self, next: &'a Policeman<'a>) { self.next = Some(next); } } fn main() { let vincent = Officer::new(8);

How do I destructure a tuple so that the bindings are mutable?

让人想犯罪 __ 提交于 2020-02-23 09:04:01
问题 If I have the following struct: struct MyStruct { tuple: (i32, i32) }; And the following function: // This will not compile fn function(&mut struct: MyStruct) { let (val1, val2) = struct.tuple; val1 = 1; val2 = 2; } How do I borrow val1 and val2 as mutable so when I reassign them the changes appear in the original struct? 回答1: You've got a few problems: You've put the &mut in the wrong place; &mut is part of the type, not the argument (unless you're destructuring the argument, which you aren

How do I destructure a tuple so that the bindings are mutable?

非 Y 不嫁゛ 提交于 2020-02-23 09:03:08
问题 If I have the following struct: struct MyStruct { tuple: (i32, i32) }; And the following function: // This will not compile fn function(&mut struct: MyStruct) { let (val1, val2) = struct.tuple; val1 = 1; val2 = 2; } How do I borrow val1 and val2 as mutable so when I reassign them the changes appear in the original struct? 回答1: You've got a few problems: You've put the &mut in the wrong place; &mut is part of the type, not the argument (unless you're destructuring the argument, which you aren

Cannot use methods from the byteorder crate on an u8 array: no method found for type in the current scope

跟風遠走 提交于 2020-02-23 07:29:10
问题 I'm trying to use a trait provided by the byteorder crate: extern crate byteorder; use byteorder::{LittleEndian, ReadBytesExt}; fn main() { let mut myArray = [0u8; 4]; myArray = [00000000, 01010101, 00100100, 11011011]; let result = myArray.read_u32::<LittleEndian>(); println!("{}", result); } I'm getting an error: error[E0599]: no method named `read_u32` found for type `[u8; 4]` in the current scope --> src/main.rs:10:26 | 10 | let result = myArray.read_u32::<LittleEndian>(); | ^^^^^^^^ | =

Streamed upload to s3 with rusoto

白昼怎懂夜的黑 提交于 2020-02-22 08:08:26
问题 How can I upload file to s3 using rusoto, without reading file content to memory (streamed)? With this code: use std::fs::File; use std::io::BufReader; use rusoto_core::Region; use rusoto_s3::{PutObjectRequest, S3, S3Client, StreamingBody}; fn main() { let file = File::open("input.txt").unwrap(); let mut reader = BufReader::new(file); let s3_client = S3Client::new(Region::UsEast1); let result = s3_client.put_object(PutObjectRequest { bucket: String::from("example_bucket"), key: "example

Streamed upload to s3 with rusoto

孤者浪人 提交于 2020-02-22 07:59:33
问题 How can I upload file to s3 using rusoto, without reading file content to memory (streamed)? With this code: use std::fs::File; use std::io::BufReader; use rusoto_core::Region; use rusoto_s3::{PutObjectRequest, S3, S3Client, StreamingBody}; fn main() { let file = File::open("input.txt").unwrap(); let mut reader = BufReader::new(file); let s3_client = S3Client::new(Region::UsEast1); let result = s3_client.put_object(PutObjectRequest { bucket: String::from("example_bucket"), key: "example

Is it possible to write a Rust macro “has_trait!(<type>,<ident>|<expr>)”?

拈花ヽ惹草 提交于 2020-02-22 07:54:13
问题 I want to match, e.g. an ident 's type to implement a certain trait, how would I do that? Here the basic idea in (incomplete) code: macro_rules! has_trait { ($ ($t : ty), ($x : ident),) => { } } fn trait_test() { let a = vec![1, 2, 3]; let b = 42; let a_iteratable = has_trait!(IntoIterator, a); let b_iteratable = has_trait!(IntoIterator, b); println!("{:?} iterable? {}", a, a_iteratable); println!("{:?} iterable? {}", b, b_iteratable); } I cannot wrap my head around how to say "any type which

Is it possible to write a Rust macro “has_trait!(<type>,<ident>|<expr>)”?

荒凉一梦 提交于 2020-02-22 07:51:04
问题 I want to match, e.g. an ident 's type to implement a certain trait, how would I do that? Here the basic idea in (incomplete) code: macro_rules! has_trait { ($ ($t : ty), ($x : ident),) => { } } fn trait_test() { let a = vec![1, 2, 3]; let b = 42; let a_iteratable = has_trait!(IntoIterator, a); let b_iteratable = has_trait!(IntoIterator, b); println!("{:?} iterable? {}", a, a_iteratable); println!("{:?} iterable? {}", b, b_iteratable); } I cannot wrap my head around how to say "any type which