rust

How can I run a set of functions on a recurring interval without running the same function at the same time using only the standard Rust library?

穿精又带淫゛_ 提交于 2021-01-03 07:33:07
问题 I would like to use Rust to create a simple scheduler in order to run multiple concurrent functions at a defined time but do not start more if they haven't finished. For example, if the defined interval is one second, the scheduler should run the functions and don't start more if the previous functions have not returned. The goal is to prevent running the same function multiple times. I created a working example with Go like this: package main import ( "fmt" "sync" "time" ) func myFunc(wg

Mutable borrow in function argument

风流意气都作罢 提交于 2021-01-03 06:41:35
问题 Why doesn't the following code compile (playground): use std::collections::HashMap; fn main() { let mut h: HashMap<u32, u32> = HashMap::new(); h.insert(0, 0); h.insert(1, h.remove(&0).unwrap()); } The borrow checker complains that: error[E0499]: cannot borrow `h` as mutable more than once at a time --> src/main.rs:6:17 | 6 | h.insert(1, h.remove(&0).unwrap()); | - ------ ^ second mutable borrow occurs here | | | | | first borrow later used by call | first mutable borrow occurs here The code

Can you control borrowing a struct vs borrowing a field?

耗尽温柔 提交于 2021-01-02 12:42:09
问题 I'm working on a program involving a struct along these lines: struct App { data: Vec<u8>, overlay: Vec<(usize, Vec<u8>)>, sink: Sink, } In brief the data field holds some bytes and overlay is a series of byte sequences to be inserted at specific indices. The Sink type is unimportant except that it has a function like: impl Sink { fn process<'a>(&mut self, input: Vec<&'a [u8]>) { // ... } } I've implemented an iterator to merge the information from data and overlay for consumption by Sink .

Can you control borrowing a struct vs borrowing a field?

本秂侑毒 提交于 2021-01-02 12:38:47
问题 I'm working on a program involving a struct along these lines: struct App { data: Vec<u8>, overlay: Vec<(usize, Vec<u8>)>, sink: Sink, } In brief the data field holds some bytes and overlay is a series of byte sequences to be inserted at specific indices. The Sink type is unimportant except that it has a function like: impl Sink { fn process<'a>(&mut self, input: Vec<&'a [u8]>) { // ... } } I've implemented an iterator to merge the information from data and overlay for consumption by Sink .

Can you control borrowing a struct vs borrowing a field?

♀尐吖头ヾ 提交于 2021-01-02 12:37:38
问题 I'm working on a program involving a struct along these lines: struct App { data: Vec<u8>, overlay: Vec<(usize, Vec<u8>)>, sink: Sink, } In brief the data field holds some bytes and overlay is a series of byte sequences to be inserted at specific indices. The Sink type is unimportant except that it has a function like: impl Sink { fn process<'a>(&mut self, input: Vec<&'a [u8]>) { // ... } } I've implemented an iterator to merge the information from data and overlay for consumption by Sink .

How do I assign a String to a mutable static variable?

点点圈 提交于 2021-01-02 07:54:37
问题 I want to assign a value to a global variable, but it keeps having a compiler error: static mut NameArr: [&'static str; 20] = ["\0"; 20]; fn main() { unsafe { static mut S1 :String = "".to_string(); S1.push('\0'); NameArr[0] = S1.as_slice(); } } The error: a.rs:7:29: 7:43 error: mutable statics are not allowed to have destructors a.rs:7 static mut S1 :String = "".to_string(); ^~~~~~~~~~~~~~ a.rs:7:29: 7:43 error: static contains unimplemented expression type [E0019] a.rs:7 static mut S1

How do I assign a String to a mutable static variable?

只谈情不闲聊 提交于 2021-01-02 07:54:17
问题 I want to assign a value to a global variable, but it keeps having a compiler error: static mut NameArr: [&'static str; 20] = ["\0"; 20]; fn main() { unsafe { static mut S1 :String = "".to_string(); S1.push('\0'); NameArr[0] = S1.as_slice(); } } The error: a.rs:7:29: 7:43 error: mutable statics are not allowed to have destructors a.rs:7 static mut S1 :String = "".to_string(); ^~~~~~~~~~~~~~ a.rs:7:29: 7:43 error: static contains unimplemented expression type [E0019] a.rs:7 static mut S1

How do I assign a String to a mutable static variable?

北城余情 提交于 2021-01-02 07:53:16
问题 I want to assign a value to a global variable, but it keeps having a compiler error: static mut NameArr: [&'static str; 20] = ["\0"; 20]; fn main() { unsafe { static mut S1 :String = "".to_string(); S1.push('\0'); NameArr[0] = S1.as_slice(); } } The error: a.rs:7:29: 7:43 error: mutable statics are not allowed to have destructors a.rs:7 static mut S1 :String = "".to_string(); ^~~~~~~~~~~~~~ a.rs:7:29: 7:43 error: static contains unimplemented expression type [E0019] a.rs:7 static mut S1

Why can the Rust compiler break borrowing rules when using Rust 1.31?

北城以北 提交于 2021-01-02 07:17:30
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow

Why can the Rust compiler break borrowing rules when using Rust 1.31?

老子叫甜甜 提交于 2021-01-02 07:15:26
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow