rust-obsolete

Return a closure from a function

淺唱寂寞╮ 提交于 2020-01-02 08:13:08
问题 Note that this question pertains to a version of Rust before 1.0 was released Do I understand correctly that it is now impossible to return a closure from a function, unless it was provided to the function in its arguments? It is very useful approach, for example, when I need the same block of code, parameterized differently, in different parts of program. Currently the compiler does not allow something like this, naturally: fn make_adder(i: int) -> |int| -> int { |j| i + j } The closure is

Conditional compilation in Rust 0.10?

a 夏天 提交于 2019-12-31 03:14:12
问题 I have been using 0.10 and recently setup a build of nightly to experiment with Box and friends. Now I have code for 0.10 using ~str and code for pre0.11 using String because of to_owned being obsolete. I thought I could do this: #[cfg(rust_version = "0.10")] fn my_old_func() -> Option<~str> { } #[cfg(not(rust_version = "0.10")] fn my_old_func() -> Option<String> { } And pass --cfg rust_version:0.11 during compilation. But the compiler still chokes on the now removed ~ operator. Is there a

Collect into owned vec of owned strings in rust

泄露秘密 提交于 2019-12-25 00:52:23
问题 I am trying to collect into an vec of strings in rust using the following: let fields : ~[~str] = row.split_str(",").collect(); I get the following error: expected std::iter::FromIterator<&str>, but found std::iter::FromIterator<~str> (str storage differs: expected & but found ~ ) I have tried to use type hints but with no success 回答1: .split_str returns an iterator over &str slices, that is, it is returning subviews of the row data. The borrowed &str is not an owned ~str : to make this work,

How would you stream output from a Process in Rust?

爷,独闯天下 提交于 2019-12-24 14:17:51
问题 This question refers to Rust as of October 2014. If you are using Rust 1.0 or above, you best look elsewhere for a solution. I have a long running Rust process that generates log values, which I'm running using Process. It looks at though I might be able to periodically "check on" the running process using set_timeout() and wait() and do something kind of high level loop like: let mut child = match Command::new("thing").arg("...").spawn() { Ok(child) => child, Err(e) => fail!("failed to

“error: trait bounds are not allowed in structure definitions” when attempting to use polymorphism

狂风中的少年 提交于 2019-12-08 19:21:09
问题 Editor's note: This question was asked before Rust 1.0 and before certain features were implemented. The code as-is works today. I'm writing a board game AI in Rust. There are multiple rulesets for the game and I'd like to have the rules logic separated from the board layout (they are currently mixed). In a language like Ruby, I'd have the separate rule sets implement the same interface. In Rust I thought about using a trait and parameterizing the Board with the ruleset I want to use (e.g.

How to rewrite code to new unboxed closures

こ雲淡風輕ζ 提交于 2019-12-06 21:12:39
问题 Can somebody help me to rewrite this piece of code with new unboxed closures: struct Builder; pub fn build(rules: |params: &mut Builder|) -> Builder { let mut builder = Builder::new(); rules(&mut builder); builder } I tried to write like this, but I got a lifetime error: pub fn build<F>(rules: F) -> Builder where F: FnOnce<(&mut Builder,), ()> { let mut builder = Builder::new(); rules(&mut builder); builder } valico/src/builder.rs:48:59: 48:71 error: missing lifetime specifier [E0106] valico

How can I generate a random number within a range in Rust?

家住魔仙堡 提交于 2019-12-06 17:06:04
问题 Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information. I came across the following example of how to generate a random number using Rust, but it doesn't appear to work. The example doesn't show which version of Rust it applies to, so perhaps it is out-of-date, or perhaps I got something wrong. // http://static.rust-lang.org

How to convert char to string?

被刻印的时光 ゝ 提交于 2019-12-06 16:36:31
问题 This Question pertains to a pre-release version of Rust. This younger Question is similar. I tried to print one symbol by io::println function fn main() { io::println('c'); } But I got next error: $ rustc pdst.rs pdst.rs:2:16: 2:19 error: mismatched types: expected `&str` but found `char` (expected &str but found char) pdst.rs:2 io::println('c'); ^~~ error: aborting due to previous error How to convert char to string? UPDATE Direct typecast does not work: let text:str = 'c'; let text:&str =

How to rewrite code to new unboxed closures

白昼怎懂夜的黑 提交于 2019-12-05 02:48:10
Can somebody help me to rewrite this piece of code with new unboxed closures: struct Builder; pub fn build(rules: |params: &mut Builder|) -> Builder { let mut builder = Builder::new(); rules(&mut builder); builder } I tried to write like this, but I got a lifetime error: pub fn build<F>(rules: F) -> Builder where F: FnOnce<(&mut Builder,), ()> { let mut builder = Builder::new(); rules(&mut builder); builder } valico/src/builder.rs:48:59: 48:71 error: missing lifetime specifier [E0106] valico/src/builder.rs:48 pub fn build<F>(rules: F) -> Builder where F: FnOnce<(&mut Builder,), ()> { ^~~~~~~~~

How can I generate a random number within a range in Rust?

廉价感情. 提交于 2019-12-04 22:44:15
Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information. I came across the following example of how to generate a random number using Rust, but it doesn't appear to work. The example doesn't show which version of Rust it applies to, so perhaps it is out-of-date, or perhaps I got something wrong. // http://static.rust-lang.org/doc/master/std/rand/trait.Rng.html use std::rand; use std::rand::Rng; fn main() { let mut rng = rand: