rust

Method `mul` has an incompatible type for trait

醉酒当歌 提交于 2020-05-17 05:12:20
问题 I'm creating a simple matrix struct in Rust and I'm trying to implement some basic operator methods: use std::ops::Mul; struct Matrix { cols: i32, rows: i32, data: Vec<f32>, } impl Matrix { fn new(cols: i32, rows: i32, data: Vec<f32>) -> Matrix { Matrix { cols: cols, rows: rows, data: data, } } } impl Mul<f32> for Matrix { type Output = Matrix; fn mul(&self, m: f32) -> Matrix { let mut new_data = Vec::with_capacity(self.cols * self.rows); for i in 0..self.cols * self.rows { new_data[i] = self

Method `mul` has an incompatible type for trait

前提是你 提交于 2020-05-17 05:11:09
问题 I'm creating a simple matrix struct in Rust and I'm trying to implement some basic operator methods: use std::ops::Mul; struct Matrix { cols: i32, rows: i32, data: Vec<f32>, } impl Matrix { fn new(cols: i32, rows: i32, data: Vec<f32>) -> Matrix { Matrix { cols: cols, rows: rows, data: data, } } } impl Mul<f32> for Matrix { type Output = Matrix; fn mul(&self, m: f32) -> Matrix { let mut new_data = Vec::with_capacity(self.cols * self.rows); for i in 0..self.cols * self.rows { new_data[i] = self

understanding error: trait `futures::future::Future` is not implemented for `()`

偶尔善良 提交于 2020-05-16 18:40:32
问题 This question is about how to read the Rust documentation and improve my understanding of Rust so as to understand how to address this specific compiler error. I've read the tokio docs and experimented with many of the examples. In writing my own code, I frequently run into compiler errors that I don't understand and often found I can fix the code, but don't understand why specific syntax is needed. I reproduced with a very simple example based on tokio's hello world: use futures::Future; use

Cannot set OpenCL kernel argument with buffer memory object

可紊 提交于 2020-05-16 07:43:08
问题 I have the following simple OpenCL kernel, that simply copies all entries pointed at a to b __kernel void mmcopy(__global float* a, __global float* b) { unsigned pos = get_global_id(0); b[pos] = a[pos]; } The following code snippet shows the opencl function calls for creating a buffer memory object out of four floats, and setting the first argument on the kernel with the buffer object. let mut v = [1f32, 1f32, 1f32, 1f32]; let size = mem::size_of_val(&v) as size_t; let mut error_buffer = 0 as

Cannot set OpenCL kernel argument with buffer memory object

天大地大妈咪最大 提交于 2020-05-16 07:42:31
问题 I have the following simple OpenCL kernel, that simply copies all entries pointed at a to b __kernel void mmcopy(__global float* a, __global float* b) { unsigned pos = get_global_id(0); b[pos] = a[pos]; } The following code snippet shows the opencl function calls for creating a buffer memory object out of four floats, and setting the first argument on the kernel with the buffer object. let mut v = [1f32, 1f32, 1f32, 1f32]; let size = mem::size_of_val(&v) as size_t; let mut error_buffer = 0 as

How do I use include_str! for multiple files or an entire directory?

巧了我就是萌 提交于 2020-05-16 04:11:10
问题 I would like to copy an entire directory to a location in a user's $HOME . Individually copying files to that directory is straightforward: let contents = include_str!("resources/profiles/default.json"); let fpath = dpath.join(&fname); fs::write(fpath, contents).expect(&format!("failed to create profile: {}", n)); I haven't found a way to adapt this to multiple files: for n in ["default"] { let fname = format!("{}{}", n, ".json"); let x = format!("resources/profiles/{}", fname).as_str(); let

How do I use include_str! for multiple files or an entire directory?

余生长醉 提交于 2020-05-16 04:10:01
问题 I would like to copy an entire directory to a location in a user's $HOME . Individually copying files to that directory is straightforward: let contents = include_str!("resources/profiles/default.json"); let fpath = dpath.join(&fname); fs::write(fpath, contents).expect(&format!("failed to create profile: {}", n)); I haven't found a way to adapt this to multiple files: for n in ["default"] { let fname = format!("{}{}", n, ".json"); let x = format!("resources/profiles/{}", fname).as_str(); let

How do I conditionally check if an enum is one variant or another?

僤鯓⒐⒋嵵緔 提交于 2020-05-15 10:37:06
问题 I have an enum with two variants: enum DatabaseType { Memory, RocksDB, } What do I need in order to make a conditional if inside a function that checks if an argument is DatabaseType::Memory or DatabaseType::RocksDB ? fn initialize(datastore: DatabaseType) -> Result<V, E> { if /* Memory */ { //.......... } else if /* RocksDB */ { //.......... } } 回答1: First, go back and re-read the free, official Rust book : The Rust Programming Language, specifically the chapter on enums. match fn initialize

What's the idiomatic way to create a iterator that owns some intermediate data and also points to it?

主宰稳场 提交于 2020-05-15 09:32:05
问题 I'm trying to create a struct that wraps around stdin to provide something like C++'s std::cin . I want to keep a String with the current line of the input and a SplitAsciiWhitespace iterator to its current token. When I reach the end of the iterator, I want to get a new line. I'm not worried about error checking and I'm not interested in any crates. This is not for production code, it's just for practicing. I want to avoid using unsafe , as a way to practice the correct mindset. The idea is

Recursive inorder traversal of a binary search tree

百般思念 提交于 2020-05-15 08:53:05
问题 I want to implement a recursive inorder in a binary search tree (BST). I built a tree using two structs: Node and Tree . My code has not worked so far, mainly because of a type mismatch in Node::inorder . pub struct Node<T> { value: T, left: Option<Box<Node<T>>>, right: Option<Box<Node<T>>>, } pub struct Tree<T> { root: Option<Box<Node<T>>>, } impl<T: Ord> Tree<T> { /// Creates an empty tree pub fn new() -> Self { Tree { root: None } } pub fn inorder(&self) -> Vec<&T> { self.root.as_ref().map