rust

Why is Rust's assert_eq! implemented using a match?

☆樱花仙子☆ 提交于 2021-01-20 15:22:46
问题 Here's Rust's assert_eq! macro implementation. I've copied only the first branch for brevity: macro_rules! assert_eq { ($left:expr, $right:expr) => ({ match (&$left, &$right) { (left_val, right_val) => { if !(*left_val == *right_val) { panic!(r#"assertion failed: `(left == right)` left: `{:?}`, right: `{:?}`"#, left_val, right_val) } } } }); } What's the purpose of the match here? Why isn't checking for non-equality enough? 回答1: Alright, let's remove the match. macro_rules! assert_eq_2 { (

Is there an easier way to make enum constants visible?

徘徊边缘 提交于 2021-01-20 06:08:21
问题 I find myself writing stuff like this: pub enum Player {BLACK, WHITE,} const BLACK: Player = Player::BLACK; const WHITE: Player = Player::WHITE; The reason, of course, being to avoid noise in match expressions and other uses of the constants. Is there an easier way to achieve this? 回答1: Yes, just import the enum variants with the use keyword. pub enum Player { Black, White, } use Player::*; 来源: https://stackoverflow.com/questions/64266286/is-there-an-easier-way-to-make-enum-constants-visible

Are there any ways to recursively flatten tuples?

北战南征 提交于 2021-01-20 05:58:14
问题 In Rust, is there any way to use trait s and impl s to (recursively) flatten tuples? If it helps, something that works with N nested pairs is a good start trait FlattenTuple { fn into_flattened(self) -> /* ??? */ } // such that assert_eq!((1, (2, 3)).into_flattened(), (1, 2, 3)) It would be even better if it could be extended work with any kind of nested tuple such that: assert_eq!(((1, 2), 2, (3, (4, 5))).into_flattened(), (1, 2, 2, 3, 4, 5)) 回答1: Maybe for certain small definitions of

Are there any ways to recursively flatten tuples?

瘦欲@ 提交于 2021-01-20 05:57:05
问题 In Rust, is there any way to use trait s and impl s to (recursively) flatten tuples? If it helps, something that works with N nested pairs is a good start trait FlattenTuple { fn into_flattened(self) -> /* ??? */ } // such that assert_eq!((1, (2, 3)).into_flattened(), (1, 2, 3)) It would be even better if it could be extended work with any kind of nested tuple such that: assert_eq!(((1, 2), 2, (3, (4, 5))).into_flattened(), (1, 2, 2, 3, 4, 5)) 回答1: Maybe for certain small definitions of

Rust: Vec<Vec<T>> into Vec<T>

你离开我真会死。 提交于 2021-01-20 04:28:32
问题 let a = vec![ vec![1, 2], vec![3, 4], vec![5, 6] ]; How can I gather into a single Vec all the values contained in all the Vec s in a ? 回答1: You can use the flatten operator to remove the nesting of the vectors. The following example is taken from the link. let data = vec![vec![1, 2, 3, 4], vec![5, 6]]; let flattened = data.into_iter().flatten().collect::<Vec<u8>>(); assert_eq!(flattened, &[1, 2, 3, 4, 5, 6]); 回答2: Steve's answer is correct, but you should also know about flat_map -- there's

How can I support an unknown or other value for a Serde enum?

六眼飞鱼酱① 提交于 2021-01-19 04:04:42
问题 I have a JSON API that returns an object that looks like this: { "PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp" } To capture this, I have an enum and a struct: #[derive(Eq, PartialEq, Deserialize, Serialize, Debug)] #[serde(rename_all = "snake_case")] pub enum PortType { Sctp, Tcp, Udp, } #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct PortMapping { pub private_port: u16, pub public_port: u16, #[serde(rename = "Type")] pub port_type: PortType, }

How can I support an unknown or other value for a Serde enum?

冷暖自知 提交于 2021-01-19 04:04:32
问题 I have a JSON API that returns an object that looks like this: { "PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp" } To capture this, I have an enum and a struct: #[derive(Eq, PartialEq, Deserialize, Serialize, Debug)] #[serde(rename_all = "snake_case")] pub enum PortType { Sctp, Tcp, Udp, } #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct PortMapping { pub private_port: u16, pub public_port: u16, #[serde(rename = "Type")] pub port_type: PortType, }

How can I support an unknown or other value for a Serde enum?

﹥>﹥吖頭↗ 提交于 2021-01-19 04:02:07
问题 I have a JSON API that returns an object that looks like this: { "PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp" } To capture this, I have an enum and a struct: #[derive(Eq, PartialEq, Deserialize, Serialize, Debug)] #[serde(rename_all = "snake_case")] pub enum PortType { Sctp, Tcp, Udp, } #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct PortMapping { pub private_port: u16, pub public_port: u16, #[serde(rename = "Type")] pub port_type: PortType, }

How to convert a boxed trait into a trait reference?

瘦欲@ 提交于 2021-01-19 03:58:29
问题 I have the following code that tries to take a reference to a trait object from a boxed trait: trait T {} struct S {} impl T for S {} fn main() { let struct_box: Box<S> = Box::new(S {}); let struct_ref: &S = &struct_box; let trait_box: Box<T> = Box::new(S {}); let trait_ref: &T = &trait_box; } The compiler returns the following error: error[E0277]: the trait bound `std::boxed::Box<T>: T` is not satisfied --> src/main.rs:12:25 | 12 | let trait_ref: &T = &trait_box; | ^^^^^^^^^^ the trait `T`

How to convert a boxed trait into a trait reference?

徘徊边缘 提交于 2021-01-19 03:57:53
问题 I have the following code that tries to take a reference to a trait object from a boxed trait: trait T {} struct S {} impl T for S {} fn main() { let struct_box: Box<S> = Box::new(S {}); let struct_ref: &S = &struct_box; let trait_box: Box<T> = Box::new(S {}); let trait_ref: &T = &trait_box; } The compiler returns the following error: error[E0277]: the trait bound `std::boxed::Box<T>: T` is not satisfied --> src/main.rs:12:25 | 12 | let trait_ref: &T = &trait_box; | ^^^^^^^^^^ the trait `T`