rust

How to write documentation tests for an internal API?

狂风中的少年 提交于 2020-01-21 09:04:47
问题 I'm writing a library which contains private structs and methods: /// Constructs a new `Object` /// /// Internal API /// /// # Example /// ```rust /// use lib::object::Object; /// /// let tn = Object::new(); /// ``` When I run cargo test , the doctest fails because Object is a private struct. Is it possible to make it compile and run ? 回答1: I don't think it is possible if you want the test to compile and run, see this related question. I you only want to include the code as a sample in the

What types of Macros/Syntax Extensions/Compiler Plugins are there?

巧了我就是萌 提交于 2020-01-21 06:32:49
问题 I am very confused by the many terms used for several macro-like things in the Rust ecosystem. Could someone clarify what macros/syntax extensions/compiler plugins there are as well as explain the relationship between those terms? 回答1: You are right: it is confusing. Especially, because most of those features are unstable and change fairly often. But I'll try to summarize the current situation ( December 2016 ). Let's start with the Syntax Extension : it's something that has to be "called" or

What types of Macros/Syntax Extensions/Compiler Plugins are there?

独自空忆成欢 提交于 2020-01-21 06:32:05
问题 I am very confused by the many terms used for several macro-like things in the Rust ecosystem. Could someone clarify what macros/syntax extensions/compiler plugins there are as well as explain the relationship between those terms? 回答1: You are right: it is confusing. Especially, because most of those features are unstable and change fairly often. But I'll try to summarize the current situation ( December 2016 ). Let's start with the Syntax Extension : it's something that has to be "called" or

Write fix point function in Rust

回眸只為那壹抹淺笑 提交于 2020-01-21 06:23:20
问题 I've just started Rust tutorial and ended with such code using recursion extern crate rand; use std::io; use rand::Rng; use std::cmp::Ordering; use std::str::FromStr; use std::fmt::{Display, Debug}; fn try_guess<T: Ord>(guess: T, actual: T) -> bool { match guess.cmp(&actual) { Ordering::Less => { println!("Too small"); false } Ordering::Greater => { println!("Too big"); false } Ordering::Equal => { println!("You win!"); true } } } fn guess_loop<T: Ord + FromStr + Display + Copy>(actual: T)

How would you stream output from a Process?

故事扮演 提交于 2020-01-21 04:22:09
问题 I believe I understand, in general, one way of doing this: Create a Command Use Stdio::piped() to create a new pair of output streams Configure command.stdout() , and command.stderr() Spawn the process Create a new thread and pass the stderr and stdout to it <-- ??? In the remote thread, continually poll for input and write it to the output stream. In the main thread, wait for the process to finish. Does that sound right? My two actual questions: Is there an easier way that doesn't involve a

How can I include private modules when generating documentation via Cargo?

亡梦爱人 提交于 2020-01-21 04:13:05
问题 I'm currently working on a project with Rust and Cargo. It works well, but I encounter a little issue: for code reuse, most of my project is inside a lib crate. In this crate, a lot of things is private. So when I do cargo doc , I just have documentation for public, exported stuff... which is actually great, because it's easy to see what is exported and what is not. But I have to admit: I miss a complete documentation of the whole project, for development purpose... 回答1: This can be done by

What is the proper way to use the `cfg!` macro to choose between multiple implementations?

被刻印的时光 ゝ 提交于 2020-01-20 09:06:19
问题 I have specified a few features inside Cargo.toml : [features] complex = [] simple = [] When I build my project I use cargo build --features="complex" or simple . In some functions, I want to return a value based on which feature is used: fn test() -> u32 { let x: u32 = 3; if cfg!(feature = "complex") { let y: u32 = 2; x + y } if cfg!(feature = "simple") { let y: u32 = 1; x + y } } But this doesn't work as it tries to evaluate both expressions. What is the proper way to use the cfg! macro in

What is the proper way to use the `cfg!` macro to choose between multiple implementations?

自闭症网瘾萝莉.ら 提交于 2020-01-20 09:04:44
问题 I have specified a few features inside Cargo.toml : [features] complex = [] simple = [] When I build my project I use cargo build --features="complex" or simple . In some functions, I want to return a value based on which feature is used: fn test() -> u32 { let x: u32 = 3; if cfg!(feature = "complex") { let y: u32 = 2; x + y } if cfg!(feature = "simple") { let y: u32 = 1; x + y } } But this doesn't work as it tries to evaluate both expressions. What is the proper way to use the cfg! macro in

How do you send slices of a Vec to a task in rust?

為{幸葍}努か 提交于 2020-01-20 08:36:48
问题 So, this doesn't work: use std::comm; #[deriving(Show)] struct St { v: u8 } fn main() { let mut foo:Vec<St> = Vec::new(); for i in range(0u8, 10) { foo.push(St { v: i }); } { let mut foo_slice = foo.as_mut_slice(); let (f1, f2) = foo_slice.split_at_mut(5); let (sl, rx):(Sender<Option<&mut [St]>>, Receiver<Option<&mut [St]>>) = comm::channel(); let (sx, rl):(Sender<bool>, Receiver<bool>) = comm::channel(); spawn(proc() { loop { let v = rx.recv(); match v { Some(v) => { v[0].v = 100u8; sx.send

rust sort

♀尐吖头ヾ 提交于 2020-01-19 18:57:38
leetcode 976 给定由一些正数(代表长度)组成的数组 A ,返回由其中三个长度组成的、面积不为零的三角形的最大周长。 如果不能形成任何面积不为零的三角形,返回 0 。 1 impl Solution { 2 pub fn largest_perimeter(a: Vec<i32>) -> i32 { 3 let mut a = a; 4 a.sort_by(|a, b| b.cmp(&a)); 5 for i in 0..a.len()-2 { 6 if a[i] < a[i+1] + a [i+2] { 7 return a[i] + a[i+1] + a[i+2]; 8 } 9 } 10 0 11 } 12 } sort_by源码 1 /// # Examples 2 /// 3 /// ``` 4 /// let mut v = [5, 4, 1, 3, 2]; 5 /// v.sort_by(|a, b| a.cmp(b)); 6 /// assert!(v == [1, 2, 3, 4, 5]); 7 /// 8 /// // reverse sorting 9 /// v.sort_by(|a, b| b.cmp(a)); 10 /// assert!(v == [5, 4, 3, 2, 1]); 11 /// ``` 12 #[stable(feature