rust

Lifetime issues with a closure argument in Rust

佐手、 提交于 2021-02-08 06:53:21
问题 I'm getting an error when trying to use a closure that does exactly the same as the print function below (in ln.9) The error is the usual borrowed value does not live long enough . I've tried to replicate this in the playground but I can't. I'm certain that this is mainly because I don't really understand what's going on here so any help would be really appreciated. What I can't understand is what is the difference between calling the print function and calling the check closure. They have

Safe and efficient way to use String and its pointer in different collections

人盡茶涼 提交于 2021-02-08 06:46:36
问题 I have a bunch of long immutable strings, which I would like to store in a HashSet . I need a bunch of mappings with these strings as keys. I would like to use references to these strings as keys in these mappings to avoid copying strings. This is how I managed to eventually get to this status. The only concern is this extra copy I need to make at line 5. let mut strings: HashSet<String> = HashSet::new(); // 1 let mut map: HashMap<&String, u8> = HashMap::new(); // 2 // 3 let s = "very long

Safe and efficient way to use String and its pointer in different collections

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 06:46:15
问题 I have a bunch of long immutable strings, which I would like to store in a HashSet . I need a bunch of mappings with these strings as keys. I would like to use references to these strings as keys in these mappings to avoid copying strings. This is how I managed to eventually get to this status. The only concern is this extra copy I need to make at line 5. let mut strings: HashSet<String> = HashSet::new(); // 1 let mut map: HashMap<&String, u8> = HashMap::new(); // 2 // 3 let s = "very long

Running Python code in parallel from Rust with rust-cpython

旧城冷巷雨未停 提交于 2021-02-08 06:14:45
问题 I'm trying to speed up a data pipeline using Rust. The pipeline contains bits of Python code that I don't want to modify, so I'm trying to run them as-is from Rust using rust-cpython and multiple threads. However, the performance is not what I expected, it's actually the same as running the python code bits sequentially in a single thread. Reading the documentation, I understand when invoking the following, you actually get a pointer to a single Python interpreter that can only be created

Running Python code in parallel from Rust with rust-cpython

我怕爱的太早我们不能终老 提交于 2021-02-08 06:13:58
问题 I'm trying to speed up a data pipeline using Rust. The pipeline contains bits of Python code that I don't want to modify, so I'm trying to run them as-is from Rust using rust-cpython and multiple threads. However, the performance is not what I expected, it's actually the same as running the python code bits sequentially in a single thread. Reading the documentation, I understand when invoking the following, you actually get a pointer to a single Python interpreter that can only be created

Why do I get “overflow evaluating the requirement” when rewriting a function using Diesel's traits into a trait method?

萝らか妹 提交于 2021-02-08 05:16:34
问题 I am trying to add pagination using Diesel. The compiler is able to check bounds on a generic type if I use a function but isn't if I try to do the same as an implementation of a trait. This is a simple working example: use diesel::query_dsl::methods::{LimitDsl, OffsetDsl}; pub fn for_page<T>(query: T) where T: OffsetDsl, T::Output: LimitDsl, { query.offset(10).limit(10); } OffsetDsl and LimitDsl are Diesel's traits which provides the methods offset and limit . When I try to extract this

Why do I get “overflow evaluating the requirement” when rewriting a function using Diesel's traits into a trait method?

断了今生、忘了曾经 提交于 2021-02-08 05:14:45
问题 I am trying to add pagination using Diesel. The compiler is able to check bounds on a generic type if I use a function but isn't if I try to do the same as an implementation of a trait. This is a simple working example: use diesel::query_dsl::methods::{LimitDsl, OffsetDsl}; pub fn for_page<T>(query: T) where T: OffsetDsl, T::Output: LimitDsl, { query.offset(10).limit(10); } OffsetDsl and LimitDsl are Diesel's traits which provides the methods offset and limit . When I try to extract this

What is GlyphCache type in a function to render text in Piston2d

别来无恙 提交于 2021-02-08 04:29:29
问题 I am trying to write a separate function to render text using piston2d. Taking the hello_world.rs example, I am trying to extend that to allow me to render text from within a function. Here is the code that I wrote: extern crate piston_window; extern crate find_folder; use piston_window::*; fn main() { let mut window: PistonWindow = WindowSettings::new( "piston: try to render text", [200, 200] ) .exit_on_esc(true) .build() .unwrap(); let assets = find_folder::Search::ParentsThenKids(3, 3)

How do I handle/circumvent “Cannot assign to … which is behind a & reference” in Rust?

▼魔方 西西 提交于 2021-02-08 04:13:42
问题 I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList<T> { start: Option<Box<Link<T>>>, } impl<T> LinkedList<T> { pub fn new() -> LinkedList<T> { return LinkedList { start: None }; } } struct Link<T> { value: Box<T>, next: Option<Box<Link<T>>>, } impl<T> Link<T> { fn new_end(value: T) -> Link<T> { return Link::new(value, None); } fn new(value: T, next: Option<Box<Link<T>>>) -> Link<T> { return Link { value: Box::new(value), next, }; } } Next on

How to initialize a struct with a series of arguments

你。 提交于 2021-02-08 04:09:42
问题 In many languages, a common constructor idiom is to initialize values of an object using syntax like this pseudocode: constructor Foo(args...) { for arg { object.arg = arg } } Rust at first seems to be no exception. Many impl for a struct include a constructor named new to zip an ordered series of arguments onto the fields of the struct: struct Circle { x: i32, y: i32, radius: i32, } impl Circle { fn new(x: i32, y: i32, radius: i32) -> Circle { Circle { x: x, y: y, radius: radius } } } Doing