rust

How can I convert a f64 to f32 and get the closest approximation and the next greater or smaller value?

落花浮王杯 提交于 2020-01-13 10:02:10
问题 Possible pseudocode for the operation could be: fn f32_greater(x: f64) -> f32 { let mut y = x as f32; //I get closest while f64::from(y) < x { y = nextafter(y, f32::INFINITY); } y } fn f32_smaller(x: f64) -> f32 { let mut y = x as f32; //I get closest while f64::from(y) > x { y = nextafter(y, f32::NEG_INFINITY); } y } I can not find an equivalent to C11's nextafter function in the libc crate or in the methods on f64 For context, I have an R-tree index using f32 . I want to search the region

How can I convert a f64 to f32 and get the closest approximation and the next greater or smaller value?

旧时模样 提交于 2020-01-13 10:02:06
问题 Possible pseudocode for the operation could be: fn f32_greater(x: f64) -> f32 { let mut y = x as f32; //I get closest while f64::from(y) < x { y = nextafter(y, f32::INFINITY); } y } fn f32_smaller(x: f64) -> f32 { let mut y = x as f32; //I get closest while f64::from(y) > x { y = nextafter(y, f32::NEG_INFINITY); } y } I can not find an equivalent to C11's nextafter function in the libc crate or in the methods on f64 For context, I have an R-tree index using f32 . I want to search the region

How can I create hygienic identifiers in code generated by procedural macros?

谁都会走 提交于 2020-01-13 09:25:12
问题 When writing a declarative ( macro_rules! ) macro, we automatically get macro hygiene . In this example, I declare a variable named f in the macro and pass in an identifier f which becomes a local variable: macro_rules! decl_example { ($tname:ident, $mname:ident, ($($fstr:tt),*)) => { impl std::fmt::Display for $tname { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let Self { $mname } = self; write!(f, $($fstr),*) } } } } struct Foo { f: String, } decl_example!(Foo, f,

Return lazy iterator that depends on data allocated within the function

风格不统一 提交于 2020-01-13 09:24:06
问题 I am new to Rust and reading The Rust Programming Language , and in the Error Handling section there is a "case study" describing a program to read data from a CSV file using the csv and rustc-serialize libraries (using getopts for argument parsing). The author writes a function search that steps through the rows of the csv file using a csv::Reader object and collect those entries whose 'city' field match a specified value into a vector and returns it. I've taken a slightly different approach

TCP tunnel over SSH in Rust

不羁的心 提交于 2020-01-13 09:16:30
问题 I'm trying to write a small program in Rust to accomplish basically what ssh -L 5000:localhost:8080 does: establish a tunnel between localhost:5000 on my machine and localhost:8080 on a remote machine, so that if an HTTP server is running on port 8080 on the remote, I can access it on my local via localhost:5000 , bypassing the remote's firewall which might be blocking external access to 8080. I realize ssh already does exactly this and reliably, this is a learning project, plus I might be

How to init a Rust vector with a generator function?

时光毁灭记忆、已成空白 提交于 2020-01-13 09:07:25
问题 Hopping between languages can be painful. Idioms of one language "feel good" and one starts to look for the same idioms in other languages. In F#, there is a way to init an array with the help of a generator function. Array.init n generator. Now, that I hopped to Rust for a little while, I wonder if there is a similar facility in place or if I have to create myself such a facility. Studying Rust standard library documentation about vectors, I could not find anything similar to what I am

How do I return a &Path from a function?

十年热恋 提交于 2020-01-13 08:52:29
问题 I'm trying to understand how to write proper Rust code, but I think I may be overestimating the power of the compiler's ability to understand the lifetimes of my objects. This is the code as I expected it to work: use std::path::Path; use std::env; use rusqlite::SqliteConnection; struct SomeDatabase { conn: SqliteConnection, } impl SomeDatabase { fn getPath() -> &Path { let path = env::home_dir().unwrap(); path.push("foo.sqlite3"); path.as_path() } fn open() -> SomeDatabase { let path =

How do I parse a page with html5ever, modify the DOM, and serialize it?

删除回忆录丶 提交于 2020-01-13 08:46:07
问题 I would like to parse a web page, insert anchors at certain positions and render the modified DOM out again in order to generate docsets for Dash. Is this possible? From the examples included in html5ever, I can see how to read an HTML file and do a poor man's HTML output, but I don't understand how I can modify the RcDom object I retrieved. I would like to see a snippet inserting an anchor element ( <a name="foo"></a> ) to an RcDom . Note: this is a question regarding Rust and html5ever

Why can I compare a String to a &str using if, but not when using match?

。_饼干妹妹 提交于 2020-01-13 08:33:11
问题 I'm trying to implement a function that reads command line arguments and compares them to hard-coded string literals. When I do the comparison with an if statement it works like a charm: fn main() { let s = String::from("holla!"); if s == "holla!" { println!("it worked!"); } } But using a match statement (which I guess would be more elegant): fn main() { let s = String::from("holla!"); match s { "holla!" => println!("it worked!"), _ => println!("nothing"), } } I keep getting an error from the

Why can I compare a String to a &str using if, but not when using match?

我怕爱的太早我们不能终老 提交于 2020-01-13 08:32:29
问题 I'm trying to implement a function that reads command line arguments and compares them to hard-coded string literals. When I do the comparison with an if statement it works like a charm: fn main() { let s = String::from("holla!"); if s == "holla!" { println!("it worked!"); } } But using a match statement (which I guess would be more elegant): fn main() { let s = String::from("holla!"); match s { "holla!" => println!("it worked!"), _ => println!("nothing"), } } I keep getting an error from the