rust

How to check if there are duplicates in a slice?

*爱你&永不变心* 提交于 2020-07-08 06:25:08
问题 Is there a native way to check if a slice has duplicates? For now, I use this: fn has_dup<T: PartialEq>(slice: &[T]) -> bool { for i in 1..slice.len() { if slice[i..].contains(&slice[i - 1]) { return true; } } false } fn main() { assert_eq!(has_dup(&[1, 2, 3, 2, 5, 6]), true); assert_eq!(has_dup(&[1, 2, 3, 4, 5, 6]), false); } but for this kind of basic operations, I do not like to use hand-made code. If there is no available function to do this in the standard library, is it a way to

How do global consts that are not copy or clone work in Rust?

二次信任 提交于 2020-07-08 03:55:10
问题 Say I have the following snippet (playground) struct A { pub val: u32 } const GLOBAL_A: A = A {val: 2}; fn main() { let some_a: A = GLOBAL_A; let other_a: A = GLOBAL_A; println!("double val = {}", some_a.val + other_a.val); } Since A is neither Clone nor Copy , I would assume the value of GLOBAL_A would be moved. That does not make much sense for a const and as shown cannot be the case anyways since it can be "moved" twice. What are the rules that allow the above snippet to work considering A

Manipulate canvas bytes directly from WebAssembly

自闭症网瘾萝莉.ら 提交于 2020-07-06 19:29:05
问题 I'm a total beginner to Rust and WebAssembly. I was trying to find a way to draw on a canvas element with performance in mind. To draw on a canvas using Rust and WebAssembly I usually find examples in which they would use the browser's CanvasRenderingContext2D interface and draw data on it they will receive from WASM const canvasContext = canvasElement.getContext("2d"); const canvasImageData = canvasContext.createImageData(width, height); const imageDataArray = getWasmImageDataArray()

Manipulate canvas bytes directly from WebAssembly

烈酒焚心 提交于 2020-07-06 19:28:51
问题 I'm a total beginner to Rust and WebAssembly. I was trying to find a way to draw on a canvas element with performance in mind. To draw on a canvas using Rust and WebAssembly I usually find examples in which they would use the browser's CanvasRenderingContext2D interface and draw data on it they will receive from WASM const canvasContext = canvasElement.getContext("2d"); const canvasImageData = canvasContext.createImageData(width, height); const imageDataArray = getWasmImageDataArray()

Manipulate canvas bytes directly from WebAssembly

烂漫一生 提交于 2020-07-06 19:28:28
问题 I'm a total beginner to Rust and WebAssembly. I was trying to find a way to draw on a canvas element with performance in mind. To draw on a canvas using Rust and WebAssembly I usually find examples in which they would use the browser's CanvasRenderingContext2D interface and draw data on it they will receive from WASM const canvasContext = canvasElement.getContext("2d"); const canvasImageData = canvasContext.createImageData(width, height); const imageDataArray = getWasmImageDataArray()

How to get struct field names in Rust? [duplicate]

安稳与你 提交于 2020-07-06 08:47:00
问题 This question already has answers here : Is there is a way to get the field names of a struct in a macro? (2 answers) Closed 4 years ago . Is there some equivalent of JS's Object.keys() for Rust's struct? I need something to generate CSV headers (I use rust-csv) from structure field names. struct Export { first_name: String, last_name: String, gender: String, date_of_birth: String, address: String } //... some code let mut wrtr = Writer::from_file("/home/me/export.csv").unwrap().delimiter(b'

Why does iterating a vector of i32s give references to i32 (&i32)?

时光毁灭记忆、已成空白 提交于 2020-07-03 11:44:07
问题 The following program tries to grade the marks of a student: use std::io; fn main() { let mut in0 = String::new(); io::stdin().read_line(&mut in0).expect("stdin err"); let n: i32 = in0.trim().parse().expect("parse err"); println!("{}", n); let mut v: Vec<i32> = Vec::new(); for _ in 0..n { let mut inp = String::new(); io::stdin().read_line(&mut inp).expect("stdin err"); let num: i32 = inp.trim().parse().unwrap(); v.push(num); } let out: Vec<_> = v .iter() .map(|x| { if x < 38 { x } else if x %

How can I specialize a method for a specific generic type?

a 夏天 提交于 2020-07-03 08:55:33
问题 I want to define methods for Untyped and Unit or basically every type, but I want to specialize a few methods just for Unit . The problem is that Rust doesn't allow two implementations here. I am using Rust 1.9, do I need to enable specialzation or is this just not possible? use num::Float; // 0.2.1 use std::marker::PhantomData; #[derive(Copy, Clone, Eq, PartialEq)] pub struct Untyped; #[derive(Copy, Clone, Eq, PartialEq)] pub struct Unit; struct Vector<T, Type> { data: [T; 3], _m:

How can I specialize a method for a specific generic type?

我与影子孤独终老i 提交于 2020-07-03 08:53:11
问题 I want to define methods for Untyped and Unit or basically every type, but I want to specialize a few methods just for Unit . The problem is that Rust doesn't allow two implementations here. I am using Rust 1.9, do I need to enable specialzation or is this just not possible? use num::Float; // 0.2.1 use std::marker::PhantomData; #[derive(Copy, Clone, Eq, PartialEq)] pub struct Untyped; #[derive(Copy, Clone, Eq, PartialEq)] pub struct Unit; struct Vector<T, Type> { data: [T; 3], _m:

Is it possible to use a variable as the fill argument in the format! macro?

大城市里の小女人 提交于 2020-07-03 08:24:20
问题 I wanted to imitate Python's rjust , ljust , and center functions using the format! macro, but I was only able to work out a solution where you can pass in the string and the width. If you want to pass in the fill-argument it doesn't work. The documentation tells me that it is possible to provide variables to format! and for the width argument it works just fine. When I try to use a variable for fill, the compiler does not recognize the pattern. Just the width as a variable works: fn rjust