rust

What does {:?} mean in a Rust format string?

房东的猫 提交于 2021-02-17 21:38:01
问题 I found out that {:?} prints an entire array in Rust. I want to know what is it called and how exactly it works. Is it only limited to printing arrays or could it also be used elsewhere for other purposes? 回答1: This is explained (along with the rest of the formatting syntax) in the std::fmt documentation. {...} surrounds all formatting directives. : separates the name or ordinal of the thing being formatted (which in this case is omitted , and thus means "the next thing") from the formatting

What does {:?} mean in a Rust format string?

江枫思渺然 提交于 2021-02-17 21:34:05
问题 I found out that {:?} prints an entire array in Rust. I want to know what is it called and how exactly it works. Is it only limited to printing arrays or could it also be used elsewhere for other purposes? 回答1: This is explained (along with the rest of the formatting syntax) in the std::fmt documentation. {...} surrounds all formatting directives. : separates the name or ordinal of the thing being formatted (which in this case is omitted , and thus means "the next thing") from the formatting

How do I bubble up an error from the closure passed to regex::Regex::replace?

那年仲夏 提交于 2021-02-17 06:42:06
问题 I have a function which performs string replacement in-place via regex::Regex::replace via a closure which performs some operations on the Captures : pub fn solve_dice_expression(expression: String) -> Result<i64, Box<dyn Error>> { lazy_static! { static ref PATTERN: Regex = Regex::new(r"(\d+)d(\d+)").expect("Problem compiling regex"); } // For every match on the Dice expression regex, roll it in-place. let rolled_expression = PATTERN.replace(&expression, |caps: &Captures| { let diceroll_str =

How do I bubble up an error from the closure passed to regex::Regex::replace?

大城市里の小女人 提交于 2021-02-17 06:42:02
问题 I have a function which performs string replacement in-place via regex::Regex::replace via a closure which performs some operations on the Captures : pub fn solve_dice_expression(expression: String) -> Result<i64, Box<dyn Error>> { lazy_static! { static ref PATTERN: Regex = Regex::new(r"(\d+)d(\d+)").expect("Problem compiling regex"); } // For every match on the Dice expression regex, roll it in-place. let rolled_expression = PATTERN.replace(&expression, |caps: &Captures| { let diceroll_str =

Process never exits after successfully receiving from MPSC channel

僤鯓⒐⒋嵵緔 提交于 2021-02-17 06:23:06
问题 Here's the code: use std::thread; use std::sync::mpsc; fn main() { //spawn threads let (tx, rx) = mpsc::channel(); for mut i in 0 .. 10 { let txc = tx.clone(); //clone from the main sender thread::spawn( move || { i += 20; println!("Sending: {}", i); txc.send(i).unwrap_or_else(|e| { eprintln!("{}", e); }); }); } for received in rx { println!("Received: {}", received); } } The code runs successfully but it hangs and the process never exits at the end. I thought it could be related to closing

Using a struct from another module in a new module

我与影子孤独终老i 提交于 2021-02-17 05:24:04
问题 The following code doesn't compile, because it says that requests is not defined when I try to use it in the operations module. I think this maybe has something to do with importing modules maybe, but I'm really new to rust and don't understand. I thought I could just do module::struct and as long as the struct was public I would be able to access it. Can someone explain why this doesn't work, and how to make this work? pub mod vehicles { pub struct Vehicle { _vehicle_id: u64, _capacity: u32,

Simulating field inheritence with composition

陌路散爱 提交于 2021-02-17 05:04:34
问题 I have several pairs of structs for which the fields of one is a perfect superset of the other. I'd like to simulate some kind of inheritance so I don't have to have separate cases for each struct since that would almost double my code. In a language like C, I could simulate inheritance of fields with something like this: struct A { int a; }; struct B { struct A parent; int b; }; main() { struct B test1; struct A *test2 = &test1; test2->a = 7; } I want to do something like this in Rust. I

UDP messages from C++ are not received by Rust

余生颓废 提交于 2021-02-17 04:48:15
问题 I'm creating a server / client paradigm using UDP, but the Rust server is not receiving the C++ client messages. I have been able to successfully do Rust server / Rust client and C++ server / Rust client communication. This leads me to believe that there is an issue with my C++ code, or there is some type of miscommunication when sending C++ buffers to Rust, but I have used code that I beleive works. This is only being sent from and to the same computer and has not been expanded to computer

Value does not live long enough when trying to set a variable outside a loop from inside the loop

北城余情 提交于 2021-02-17 04:29:43
问题 I'm making a Discord chat bot using discord-rs, starting from this example. Everything was working and compiling fine until I tried to modify a value that is declared before a loop. I'm trying to change prefix to the second word that is entered in a command: extern crate discord; use discord::Discord; use discord::model::Event; use std::env; fn main() { let discord = Discord::from_bot_token(&env::var("DISCORD_TOKEN").expect("Expected token")) .expect("login failed"); let (mut connection, _) =

Value does not live long enough when trying to set a variable outside a loop from inside the loop

白昼怎懂夜的黑 提交于 2021-02-17 04:29:37
问题 I'm making a Discord chat bot using discord-rs, starting from this example. Everything was working and compiling fine until I tried to modify a value that is declared before a loop. I'm trying to change prefix to the second word that is entered in a command: extern crate discord; use discord::Discord; use discord::model::Event; use std::env; fn main() { let discord = Discord::from_bot_token(&env::var("DISCORD_TOKEN").expect("Expected token")) .expect("login failed"); let (mut connection, _) =