rust

Is it valid to wake a Rust future while it's being polled?

浪尽此生 提交于 2020-07-18 11:31:27
问题 I'd like to be able to sleep my future for a single "frame" so that other work can happen. Is this a valid implementation of this idea? use std::future::Future; use std::task::{Context, Poll}; use std::pin::Pin; struct Yield { yielded: bool, } impl Future for Yield { type Output = (); fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<()> { if self.yielded { Poll::Ready(()) } else { self.yielded = true; // This is the part I'm concerned about ctx.waker().wake_by_ref(); Poll::Pending

Difficulty aggregating matches with |

喜夏-厌秋 提交于 2020-07-18 07:59:06
问题 I'm trying to pack columnar data so that it can be sent to a server. I wanted to be able to pass any suitable vector to the sending function, so I did this (brief version): enum Column { Short(Vec<i16>), Int(Vec<i32>), } impl Column { fn as_bytes(&mut self) -> &[u8] { use Column::*; // weird match self { Short(vec) => unsafe { (vec.align_to::<u8>()).1 }, //why the ::? Int(vec) => unsafe { (vec.align_to::<u8>()).1 }, } } } This works ok. However, if I rewrite the match with an or pipe: impl

Why is my Rust version of “wc” slower than the one from GNU coreutils?

时间秒杀一切 提交于 2020-07-18 05:06:07
问题 Consider this program: use std::io::BufRead; use std::io; fn main() { let mut n = 0; let stdin = io::stdin(); for _ in stdin.lock().lines() { n += 1; } println!("{}", n); } Why is it over 10x as slow as the GNU version of wc? Take a look at how I measure it: $ yes | dd count=1000000 | wc -l 256000000 1000000+0 records in 1000000+0 records out 512000000 bytes (512 MB, 488 MiB) copied, 1.16586 s, 439 MB/s $ yes | dd count=1000000 | ./target/release/wc 1000000+0 records in 1000000+0 records out

How to remove Rust compiler toolchains with Rustup?

一曲冷凌霜 提交于 2020-07-18 03:28:27
问题 The Rustup documentation shows how to install Rust nightly, but not how to remove it. While the docs do show how to uninstall rustup entirely, I'd like to keep the stable branch. How can I uninstall Rust nightly? Note that I attempted to do the opposite of rustup install nightly ... ( rustup uninstall nightly , rustup remove nightly & rustup delete nightly ) to no avail. Even though I read the documentation it wasn't clear that nightly was a toolchain , a channel ... or something else. 回答1:

How to remove Rust compiler toolchains with Rustup?

会有一股神秘感。 提交于 2020-07-18 03:27:49
问题 The Rustup documentation shows how to install Rust nightly, but not how to remove it. While the docs do show how to uninstall rustup entirely, I'd like to keep the stable branch. How can I uninstall Rust nightly? Note that I attempted to do the opposite of rustup install nightly ... ( rustup uninstall nightly , rustup remove nightly & rustup delete nightly ) to no avail. Even though I read the documentation it wasn't clear that nightly was a toolchain , a channel ... or something else. 回答1:

What does Some() do on the left hand side of a variable assignment?

坚强是说给别人听的谎言 提交于 2020-07-15 09:59:07
问题 I was reading some Rust code and I came across this line if let Some(path) = env::args().nth(1) { Inside of this function fn main() { if let Some(path) = env::args().nth(1) { // Try reading the file provided by the path. let mut file = File::open(path).expect("Failed reading file."); let mut content = String::new(); file.read_to_string(&mut content); perform_conversion(content.as_str()).expect("Conversion failed."); } else { println!( "provide a path to a .cue file to be converted into a

Printing a character a variable number of times with println

吃可爱长大的小学妹 提交于 2020-07-15 07:06:52
问题 I want to use println! and the powerful formatting tools of format! to print a character a specific number of times. Of course this is possible with a loop, like so: fn give_love(count: usize) { print!("Here is love for you: "); for i in 0..count { print!("♥"); } println!(""); } But I neither want to write the loop nor the three print s. How to do this shorter/better? 回答1: Solution to your code fn give_love(count: usize) { println!("Here is love for you: {:♥<1$}", "", count); } Explanation

What is the motivation of Rust's ToLowercase

一世执手 提交于 2020-07-14 07:22:27
问题 Rust's char has a to_lowercase function which seems to return the struct ToLowercase which seems to be an iterator with always one element. Wouldn't returning a char directly be far more natural and simple? 回答1: Wouldn't returning a char directly be far more natural and simple? Natural, simple, and wrong. Unicode is too complicated for that to work. The fundamental issue is that a char is not sufficient to always represent a single, logically complete "character", for some definitions of

Split a string and return Vec<String>

蹲街弑〆低调 提交于 2020-07-13 10:04:32
问题 I want to split a string and return Vec<String> from my function. It has to be Vec<String> and not Vec<&str> because I can't return Vec<&str> , can I? If I can, though, how can I do that? let var1: Vec<&str> = my_string.split("something").collect(); let res = var1.iter().map(|x| x.to_string()); // I want to return Vec<String> I've tried different versions but gotten error: mismatched types and other kinds of similar errors. Is there an easier way? 回答1: You don't need to create an intermediate

Split a string and return Vec<String>

泪湿孤枕 提交于 2020-07-13 10:04:07
问题 I want to split a string and return Vec<String> from my function. It has to be Vec<String> and not Vec<&str> because I can't return Vec<&str> , can I? If I can, though, how can I do that? let var1: Vec<&str> = my_string.split("something").collect(); let res = var1.iter().map(|x| x.to_string()); // I want to return Vec<String> I've tried different versions but gotten error: mismatched types and other kinds of similar errors. Is there an easier way? 回答1: You don't need to create an intermediate