rust

How to suppress “function is never used” warning for a function used by tests?

情到浓时终转凉″ 提交于 2020-01-10 09:37:12
问题 I'm writing a program in Rust and I have some tests for it. I wrote a helper function for these tests, but whenever I build using cargo build it warns me that the function is never used: warning: function is never used: ... #[warn(dead_code)] on by default How I can mark this function as used so as not to get the warnings? 回答1: Specific question How I can mark this function as used so as not to get the warnings? The Rust compiler runs many lints to warn you about possible issues in your code

How to suppress “function is never used” warning for a function used by tests?

ε祈祈猫儿з 提交于 2020-01-10 09:37:09
问题 I'm writing a program in Rust and I have some tests for it. I wrote a helper function for these tests, but whenever I build using cargo build it warns me that the function is never used: warning: function is never used: ... #[warn(dead_code)] on by default How I can mark this function as used so as not to get the warnings? 回答1: Specific question How I can mark this function as used so as not to get the warnings? The Rust compiler runs many lints to warn you about possible issues in your code

How to suppress “function is never used” warning for a function used by tests?

自闭症网瘾萝莉.ら 提交于 2020-01-10 09:34:56
问题 I'm writing a program in Rust and I have some tests for it. I wrote a helper function for these tests, but whenever I build using cargo build it warns me that the function is never used: warning: function is never used: ... #[warn(dead_code)] on by default How I can mark this function as used so as not to get the warnings? 回答1: Specific question How I can mark this function as used so as not to get the warnings? The Rust compiler runs many lints to warn you about possible issues in your code

Lifetimes in Rust

試著忘記壹切 提交于 2020-01-10 06:52:07
问题 Occasionally I've found myself wanting to write functions that can be called in either of two ways: // With a string literal: let lines = read_file_lines("data.txt"); // With a string pointer: let file_name = ~"data.txt"; let lines = read_file_lines(file_name); My first guess was to use a borrowed pointer ( &str ) for the parameter type, but when that didn't work (it only allowed me to use @str and ~str ), I tried the following (by copying the Rust libraries), which did work. fn read_file

How do I implement FromStr with a concrete lifetime?

匆匆过客 提交于 2020-01-10 05:10:49
问题 I want to implement FromStr for a struct with a lifetime parameter: use std::str::FromStr; struct Foo<'a> { bar: &'a str, } impl<'a> FromStr for Foo<'a> { type Err = (); fn from_str(s: &str) -> Result<Foo<'a>, ()> { Ok(Foo { bar: s }) } } pub fn main() { let foo: Foo = "foobar".parse().unwrap(); } However, the compiler complains: error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements --> src/main.rs:11:12 | 11 | Ok(Foo { bar: s }) | ^^^ | help: consider using an

Is there a built-in function that converts a number to a string in any base?

淺唱寂寞╮ 提交于 2020-01-10 04:59:06
问题 I want to replace the inner match statement and work for all values up to when the alphabet runs out. I know I can write it myself, but I want to use built-in functions. fn convert(inp: u32, out: u32, numb: &String) -> Result<String, String> { match isize::from_str_radix(numb, inp) { Ok(a) => match out { 2 => Ok(format!("{:b}", a)), 8 => Ok(format!("{:o}", a)), 16 => Ok(format!("{:x}", a)), 10 => Ok(format!("{}", a)), 0 | 1 => Err(format!("No base lower than 2!")), _ => Err(format!("printing

Is there any way to return from a function from inside a closure?

喜欢而已 提交于 2020-01-10 04:58:07
问题 I have the following simplified code: fn f() -> i32 { let a = some_result.unwrap_or_else(|_| { return 1; // want to return this value from f <------------- }); } I want to return the value 1 from the whole function f in this specific error case but I can't figure out how to do it from within a closure. If I instead use a match expression, it works fine as follows: fn f() -> i32 { let a = match some_result { Ok(result) => result, Err(_) => { return 1; }, }; } However, this makes the code

Reading from a TcpStream with Read::read_to_string hangs until the connection is closed by the remote end

孤街醉人 提交于 2020-01-10 04:49:04
问题 I'm attempting to implement the Haskell IRC bot tutorial in Rust and am having some difficulty reading what the server sends me after connecting. What seems to happen is that I connect, read ~5 KB from the server, and then roughly 240 seconds later everything is dumped at once instead of being read line-by-line. The connection is closed by a ping timeout, which should happen eventually, since I don't yet have a ping-pong function to reply with. Here's what I have so far: use std::io::{Read,

Macro for defining trait aliases

拥有回忆 提交于 2020-01-10 04:03:06
问题 According to this isuue issue and this answered question it is not possible to simply define a trait alias like: trait Alias = Foo + Bar; The workaround is a bit ugly: trait Alias : Foo + Bar {} impl<T: Foo + Bar> Alias for T {} Therefore I want to define a macro for this. I tried macro_rules! trait_alias { ( $name : ident, $base : expr ) => { trait $name : $base {} impl<T: $base> $name for T {} }; } trait Foo {} trait Bar {} trait_alias!(Alias, Foo + Bar); But it fails with error: src\main

Why would you ever use the same lifetimes for references in a struct?

核能气质少年 提交于 2020-01-10 03:46:11
问题 This question is similar to When is it useful to define multiple lifetimes in a struct?, but hopefully different enough. The answer to that question is helpful but focuses on advantages of one approach (using distinct lifetimes for references in struct) but not on drawbacks (if any). This question, like that, is looking for guidance on how to choose lifetimes when creating structs. Call this the tied together version because x and y are required to have the same lifetime: struct Foo<'a> { x: