rust

How do I get a vector of u8 RGB values when using the image crate?

て烟熏妆下的殇ゞ 提交于 2020-01-21 16:35:51
问题 I need to take an image and get a list of RGB byte values. I am using the image crate. This is what I have: extern crate image; fn main() { let im = image::open("wall.jpg").unwrap().to_rgb(); let data: Vec<[u8; 3]> = im.pixels().flat_map(|p| vec![p.data]).collect(); let rgb: Vec<&u8> = data.iter().flat_map(|p| p.iter()).collect(); println!("First Pixel: {} {} {}", rgb[0], rgb[1], rgb[2]); } This seems pretty ugly. I have to introduce an intermediate variable and I get a vector of pointers to

How to get an Option's value or set it if it's empty?

一个人想着一个人 提交于 2020-01-21 16:18:21
问题 I want to get the name if it's not empty or set a new value. How can I do that? #[derive(Debug)] struct App { name: Option<String>, age: i32, } impl App { fn get_name<'a>(&'a mut self) -> &'a Option<String> { match self.name { Some(_) => &self.name, None => { self.name = Some(String::from("234")); &self.name } } } } fn main() { let mut app = App { name: None, age: 10, }; println!("{:?} and name is {}", &app, &app.get_name().unwrap()) } The error I'm getting is: error[E0507]: cannot move out

What is this strange syntax where an enum variant is used as a function?

我的梦境 提交于 2020-01-21 12:53:01
问题 Below is the example given by the mod documentation of syn::parse . enum Item { Struct(ItemStruct), Enum(ItemEnum), } struct ItemStruct { struct_token: Token![struct], ident: Ident, brace_token: token::Brace, fields: Punctuated<Field, Token![,]>, } impl Parse for Item { fn parse(input: ParseStream) -> Result<Self> { let lookahead = input.lookahead1(); if lookahead.peek(Token![struct]) { input.parse().map(Item::Struct) // <-- here } else if lookahead.peek(Token![enum]) { input.parse().map(Item

What is this strange syntax where an enum variant is used as a function?

纵然是瞬间 提交于 2020-01-21 12:52:58
问题 Below is the example given by the mod documentation of syn::parse . enum Item { Struct(ItemStruct), Enum(ItemEnum), } struct ItemStruct { struct_token: Token![struct], ident: Ident, brace_token: token::Brace, fields: Punctuated<Field, Token![,]>, } impl Parse for Item { fn parse(input: ParseStream) -> Result<Self> { let lookahead = input.lookahead1(); if lookahead.peek(Token![struct]) { input.parse().map(Item::Struct) // <-- here } else if lookahead.peek(Token![enum]) { input.parse().map(Item

What is this strange syntax where an enum variant is used as a function?

旧街凉风 提交于 2020-01-21 12:52:42
问题 Below is the example given by the mod documentation of syn::parse . enum Item { Struct(ItemStruct), Enum(ItemEnum), } struct ItemStruct { struct_token: Token![struct], ident: Ident, brace_token: token::Brace, fields: Punctuated<Field, Token![,]>, } impl Parse for Item { fn parse(input: ParseStream) -> Result<Self> { let lookahead = input.lookahead1(); if lookahead.peek(Token![struct]) { input.parse().map(Item::Struct) // <-- here } else if lookahead.peek(Token![enum]) { input.parse().map(Item

How to put heterogeneous types into a Rust structure [duplicate]

南笙酒味 提交于 2020-01-21 12:32:42
问题 This question already has an answer here : How do I create a heterogeneous collection of objects? (1 answer) Closed 2 years ago . My questions is two parts (since I couldn't get the first part, I moved to the second part, which still left me with questions) Part #1: How do you insert heterogeneous struct types into a HashMap ? At first I thought to do it via an enum E.g., enum SomeEnum { TypeA, TypeB, TypeC, } struct TypeA{} struct TypeB{} struct TypeC{} let hm = HashMap::new(); hm.insert(

How to put heterogeneous types into a Rust structure [duplicate]

穿精又带淫゛_ 提交于 2020-01-21 12:31:32
问题 This question already has an answer here : How do I create a heterogeneous collection of objects? (1 answer) Closed 2 years ago . My questions is two parts (since I couldn't get the first part, I moved to the second part, which still left me with questions) Part #1: How do you insert heterogeneous struct types into a HashMap ? At first I thought to do it via an enum E.g., enum SomeEnum { TypeA, TypeB, TypeC, } struct TypeA{} struct TypeB{} struct TypeC{} let hm = HashMap::new(); hm.insert(

How can I store multiple elements in a Rust HashMap for the same key?

淺唱寂寞╮ 提交于 2020-01-21 12:23:26
问题 I have a HashMap<u32, Sender> . Sender is a open connection object and the key is a user id. Each user can connect from multiple devices. I need to store all possible open connections for the same user id. After this I can iterate and send messages to all open connections for same user. The above HashMap only stores each user id and connection once. I need to get one key with multiple values. How can I make the value into a list or an array, so I can see which connections exist and send to

Is it possible to create a mutable value of a mutable reference in a pattern?

跟風遠走 提交于 2020-01-21 11:10:33
问题 When pattern-matching, you can specify that you'd like to get a mutable reference to the contained value by using ref mut : let mut score = Some(42); if let Some(ref mut s) = score { &mut s; } However, the inner value is not mutable: error[E0596]: cannot borrow immutable local variable `s` as mutable --> src/main.rs:4:14 | 4 | &mut s; | ^ | | | cannot reborrow mutably | try removing `&mut` here I tried to add in another mut , but that was not valid: if let Some(mut ref mut s) = score { &mut s

Is it possible to create a mutable value of a mutable reference in a pattern?

空扰寡人 提交于 2020-01-21 11:08:23
问题 When pattern-matching, you can specify that you'd like to get a mutable reference to the contained value by using ref mut : let mut score = Some(42); if let Some(ref mut s) = score { &mut s; } However, the inner value is not mutable: error[E0596]: cannot borrow immutable local variable `s` as mutable --> src/main.rs:4:14 | 4 | &mut s; | ^ | | | cannot reborrow mutably | try removing `&mut` here I tried to add in another mut , but that was not valid: if let Some(mut ref mut s) = score { &mut s