rust

Is it possible to use a single generic for both key and value of a HashMap?

风流意气都作罢 提交于 2021-02-19 06:04:57
问题 In chapter 13 of the Rust book, you implement a Cacher to use memoization to demonstrate functional programming and how to speed up long-running tasks. As an extra challenge, they recommend making the Cacher allow multiple keys using a HashMap and also leveraging generics to allow more flexibility. Try modifying Cacher to hold a hash map rather than a single value. The keys of the hash map will be the arg values that are passed in, and the values of the hash map will be the result of calling

How can I filter an iterator when the predicate returns a Result<bool, _>?

こ雲淡風輕ζ 提交于 2021-02-19 05:00:05
问题 I would like the iterator to be filtered, but my predicate has the possibility of failing. When the predicate fails, I'd like to fail the entire function. In this example, I'd like work to return the Result generated by maybe : fn maybe(v: u32) -> Result<bool, u8> { match v % 3 { 0 => Ok(true), 1 => Ok(false), 2 => Err(42), } } fn work() -> Result<Vec<u32>, u8> { [1, 2, 3, 4, 5].iter().filter(|&&x| maybe(x)).collect() } fn main() { println!("{:?}", work()) } error[E0308]: mismatched types -->

Is a returned value moved or not?

孤街浪徒 提交于 2021-02-19 04:38:27
问题 With this code: struct Point { x: f64, y: f64, } struct Rectangle { p1: Point, p2: Point, } impl Rectangle { pub fn new(x1: f64, y1: f64, x2: f64, y2: f64) -> Rectangle { let r = Rectangle { p1: Point { x: x1, y: y1 }, p2: Point { x: x2, y: y2 }, }; // some code where r is used r } } let rectangle = Rectangle::new(0.0, 0.0, 10.0, 10.0); From a memory point of view, is rectangle the same instance as r , or is it a copy of r ? Do I have to explicitly return by reference (something like &r )? I

Creating a Custom Colored dbg! Macro In Rust

隐身守侯 提交于 2021-02-19 03:28:52
问题 I'd like to create a custom macro similar to the standard dbg! macro, but with the option to use colors via the colored crate. dbg! usually prints something with the format of [path_to_file:line_number] "symbol name" = "symbol value" //[src/gallery/image_slot.rs:231] "my_integer_value_of_12" = "12" How do I access the path/line number [path_to_file:line_number] so I can print it? How do I access the symbol name of a variable? (i.e. print my_var given my_var = 12 ) 回答1: Use the file!, line!,

How to get only the directory portion of the current executable's path?

六月ゝ 毕业季﹏ 提交于 2021-02-18 22:45:33
问题 I want to read files from a config folder at the directory where the executable is located. I do that using the following functions: use std::env; // add part of path to te path gotten from fn get_exe_path(); fn get_file_path(path_to_file: &str) -> PathBuf { let final_path = match get_exe_path() { Ok(mut path) => { path.push(path_to_file); path } Err(err) => panic!("Path does not exists"), }; final_path } // Get path to current executable fn get_exe_path() -> Result<PathBuf, io::Error> { /

How to get only the directory portion of the current executable's path?

瘦欲@ 提交于 2021-02-18 22:43:20
问题 I want to read files from a config folder at the directory where the executable is located. I do that using the following functions: use std::env; // add part of path to te path gotten from fn get_exe_path(); fn get_file_path(path_to_file: &str) -> PathBuf { let final_path = match get_exe_path() { Ok(mut path) => { path.push(path_to_file); path } Err(err) => panic!("Path does not exists"), }; final_path } // Get path to current executable fn get_exe_path() -> Result<PathBuf, io::Error> { /

How to get only the directory portion of the current executable's path?

本秂侑毒 提交于 2021-02-18 22:43:18
问题 I want to read files from a config folder at the directory where the executable is located. I do that using the following functions: use std::env; // add part of path to te path gotten from fn get_exe_path(); fn get_file_path(path_to_file: &str) -> PathBuf { let final_path = match get_exe_path() { Ok(mut path) => { path.push(path_to_file); path } Err(err) => panic!("Path does not exists"), }; final_path } // Get path to current executable fn get_exe_path() -> Result<PathBuf, io::Error> { /

Overloading an operator for all structs with a trait in Rust

流过昼夜 提交于 2021-02-18 21:23:29
问题 I'm trying to implement C++-style expression templates in Rust using traits and operator overloading. I'm getting stuck trying to overload '+' and '*' for every expression template struct. The compiler complains about the Add and Mul trait implementations: error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`) --> src/main.rs:32:6 | 32 | impl<T: HasValue + Copy, O: HasValue + Copy> Add<O> for T { | ^ type parameter `T` must be used as the

Mysterious borrow scope extension

旧巷老猫 提交于 2021-02-18 21:10:39
问题 Why does the compiler reject this code: struct S<'a> { i: i32, r: &'a i32, } fn main() { let mut s = S{i: 0, r: &0}; { let m1 = &mut s; m1.r = &m1.i; } let m2 = &mut s; } The error is: "cannot borrow s as mutable more than once at a time" (first borrow: m1 , second borrow: m2 ). Why is the first borrow of s still alive after m1 goes out of scope? I read about borrow scope extension beyond the scope of the original borrower. However, this always seemed to involve another borrower outside the

Mysterious borrow scope extension

这一生的挚爱 提交于 2021-02-18 21:08:21
问题 Why does the compiler reject this code: struct S<'a> { i: i32, r: &'a i32, } fn main() { let mut s = S{i: 0, r: &0}; { let m1 = &mut s; m1.r = &m1.i; } let m2 = &mut s; } The error is: "cannot borrow s as mutable more than once at a time" (first borrow: m1 , second borrow: m2 ). Why is the first borrow of s still alive after m1 goes out of scope? I read about borrow scope extension beyond the scope of the original borrower. However, this always seemed to involve another borrower outside the