rust

How does String::from(“”) & “”.to_string() differ in Rust? [duplicate]

懵懂的女人 提交于 2020-06-16 09:39:54
问题 This question already has answers here : How to create a String directly? (3 answers) What is the difference between these 3 ways of declaring a string in Rust? (1 answer) Closed last month . How does String::from("") & "".to_string() differ in Rust? Is there any difference in stack and heap allocation in both cases? 回答1: How does String::from("") & "".to_string() differ in Rust? They're part of different protocols (traits): std::convert::From and alloc::string::ToString[0]. However, when it

How can I convert a std::process::Command into a command line string?

心不动则不痛 提交于 2020-06-16 04:33:51
问题 For example: let mut com = std::process::Command::new("ProgramA"); com.env("ENV_1", "VALUE_1") .arg("-a") .arg("foo") .arg("-b") .arg("--argument=bar"); // Get the command line string somehow here. com.output().unwrap(); This will spawn a process with this command line "ProgramA" -a foo -b "--argument=with space" associated with it. Is there a way to get this back out from the com object? 回答1: It turns out Command implements Debug ; this will give me the desired result: let answer = format!("

Why can't I create a trait object with let _: Arc<dyn Trait> = value.into()?

試著忘記壹切 提交于 2020-06-16 02:49:24
问题 use std::sync::Arc; trait Trait {} struct TraitImpl {} impl Trait for TraitImpl {} fn main() { let value = TraitImpl {}; let _: Arc<dyn Trait> = Arc::new(value); // compiles let _: Arc<dyn Trait> = value.into(); // doesn't compile } Result: error[E0277]: the trait bound `std::sync::Arc<dyn Trait>: std::convert::From<TraitImpl>` is not satisfied --> src/main.rs:10:35 | 10 | let _: Arc<dyn Trait> = value.into(); // doesn't compile | ^^^^ the trait `std::convert::From<TraitImpl>` is not

How do I convert an iterator into a stream on success or an empty stream on failure?

孤人 提交于 2020-06-15 06:24:33
问题 I'd like to take a regular iterator and turn it into a stream so that I can do further stream processing. The trouble is that I may have an iterator or an error to deal with. I think I'm pretty close with this: #[macro_use] extern crate log; extern crate futures; // 0.1.21 extern crate tokio; use futures::prelude::*; use futures::{future, stream}; use std::fmt::Debug; use std::net::{SocketAddr, ToSocketAddrs}; fn resolve(addrs: impl ToSocketAddrs + Debug) -> impl Stream<Item = SocketAddr,

Idiomatic way to count occurrences in a collection of Options

左心房为你撑大大i 提交于 2020-06-14 06:56:12
问题 I want to count number of occurrences of a value in a collection of Options. let v = vec![Some(1), Some(1), Some(3), None]; v.iter() .filter(|Some(x)| x == &1) .count(); Doing this gives refutable pattern not covered error which makes sense. I got around this by doing v.iter() .filter(|x| x.is_some() && x.unwrap() == &1) .count() What's the idiomatic way to do this in rust? 回答1: You can use flatten to get rid of None and unwrap the Some(...) values. Code: let one_count = v.iter().flatten()

Couldn't start client Rust Language Server

让人想犯罪 __ 提交于 2020-06-14 06:43:59
问题 I'm trying to figure out how to use rustc & cargo from my WSL. I use VS Code and Rust (rls) plugin and can compile my code but there is a problem with RLS: Couldn't start client Rust Language Server Rustup not available. Install from https://www.rustup.rs/ How i can solve this problem? 回答1: I had this problem as well with WSL and Visual Studio Code. The problem seems to stem from the fact that the Rust Language Server needs to find rustup in your path. We both probably followed the same path

Couldn't start client Rust Language Server

点点圈 提交于 2020-06-14 06:43:31
问题 I'm trying to figure out how to use rustc & cargo from my WSL. I use VS Code and Rust (rls) plugin and can compile my code but there is a problem with RLS: Couldn't start client Rust Language Server Rustup not available. Install from https://www.rustup.rs/ How i can solve this problem? 回答1: I had this problem as well with WSL and Visual Studio Code. The problem seems to stem from the fact that the Rust Language Server needs to find rustup in your path. We both probably followed the same path

What is the evaluation order of tuples in Rust?

半世苍凉 提交于 2020-06-14 05:13:33
问题 Tuple elements may have side-effects, and some of them may depend on others. Consider this program: fn main() { let mut v = vec![1, 2]; match (v.pop(), v.pop()) { (Some(z), Some(y)) => println!("y = {}, z = {}", y, z), _ => unreachable!(), } } Does it output y = 1, z = 2 or y = 2, z = 1 ? A few rounds on the Rust Playground suggests the former on stable 1.32.0, but maybe it would change if I ran it more times, recompiled the compiler, changed compiler versions, etc. Is there a documented

What is the evaluation order of tuples in Rust?

≡放荡痞女 提交于 2020-06-14 05:10:31
问题 Tuple elements may have side-effects, and some of them may depend on others. Consider this program: fn main() { let mut v = vec![1, 2]; match (v.pop(), v.pop()) { (Some(z), Some(y)) => println!("y = {}, z = {}", y, z), _ => unreachable!(), } } Does it output y = 1, z = 2 or y = 2, z = 1 ? A few rounds on the Rust Playground suggests the former on stable 1.32.0, but maybe it would change if I ran it more times, recompiled the compiler, changed compiler versions, etc. Is there a documented

Why is immutability enforced in Rust unless otherwise specified with `mut`?

谁说我不能喝 提交于 2020-06-13 15:34:08
问题 Why is immutability forced in Rust, unless you specify mut ? Is this a design choice for safety, do you consider this how it should be naturally in other languages? I should probably clarify, I'm still a newbie at Rust. So is this a design choice related to another feature in the language? 回答1: The Rust-Book actually addresses this topic. There is no single reason that bindings are immutable by default, but we can think about it through one of Rust’s primary focuses: safety. If you forget to