rust

How do I create a Rust macro with optional parameters using repetitions?

帅比萌擦擦* 提交于 2020-05-26 11:22:58
问题 I'm currently looking into Rust macros and I can not find any detailed documentation on repetitions. I would like to create macros with optional parameters. This would be my idea: macro_rules! single_opt { ($mand_1, $mand_2, $($opt:expr)* ) =>{ match $opt { Some(x) => println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, x); None => single_opt!($mand_1, $mand_2, "Default"); } } } fn main() { single_opt!(4,4); } This example seems to be outdated, since I can not compile it. The Rust book mentions

How do I create a Rust macro with optional parameters using repetitions?

丶灬走出姿态 提交于 2020-05-26 11:22:20
问题 I'm currently looking into Rust macros and I can not find any detailed documentation on repetitions. I would like to create macros with optional parameters. This would be my idea: macro_rules! single_opt { ($mand_1, $mand_2, $($opt:expr)* ) =>{ match $opt { Some(x) => println!("1. {} 2. {}, 3. {}", $mand_1, $mand_2, x); None => single_opt!($mand_1, $mand_2, "Default"); } } } fn main() { single_opt!(4,4); } This example seems to be outdated, since I can not compile it. The Rust book mentions

How can I co-sort two Vecs based on the values in one of the Vecs?

倾然丶 夕夏残阳落幕 提交于 2020-05-26 10:32:16
问题 I have two Vec s that correspond to a list of feature vectors and their corresponding class labels, and I'd like to co-sort them by the class labels. However, Rust's sort_by operates on a slice rather than being a generic function over a trait (or similar), and the closure only gets the elements to be compared rather than the indices so I can sneakily hack the sort to be parallel. I've considered the solution: let mut both = data.iter().zip(labels.iter()).collect(); both.sort_by( blah blah );

Is it possible to check whether a C macro is defined on your system in Rust?

匆匆过客 提交于 2020-05-26 09:15:09
问题 I'm aware that the libc crate in Rust contains much of C's standard macros and functions for use in Rust, but it also states that it is not concerned with portability between systems. I'm porting some code that uses C's preprocessor macros extremely heavily from C to Rust, and only includes some code if a given macro is defined: in this case O_BINARY . Is it possible to check whether the O_BINARY macro is defined on my system in Rust, and if so, what does this look like? I'm looking for a

Is it possible to check whether a C macro is defined on your system in Rust?

天涯浪子 提交于 2020-05-26 09:14:22
问题 I'm aware that the libc crate in Rust contains much of C's standard macros and functions for use in Rust, but it also states that it is not concerned with portability between systems. I'm porting some code that uses C's preprocessor macros extremely heavily from C to Rust, and only includes some code if a given macro is defined: in this case O_BINARY . Is it possible to check whether the O_BINARY macro is defined on my system in Rust, and if so, what does this look like? I'm looking for a

Temporary value is freed at the end of this statement [duplicate]

元气小坏坏 提交于 2020-05-25 07:10:30
问题 This question already has answers here : “borrowed value does not live long enough” when using the builder pattern (1 answer) “borrowed value does not live long enough” seems to blame the wrong thing (2 answers) Why is it legal to borrow a temporary? (3 answers) Why does the compiler tell me to consider using a `let` binding" when I already am? (2 answers) Closed last year . I'm trying to scrape a webpage using the Select crate: let document = Document::from_read(response).unwrap(); for node

Temporary value is freed at the end of this statement [duplicate]

ぐ巨炮叔叔 提交于 2020-05-25 07:09:13
问题 This question already has answers here : “borrowed value does not live long enough” when using the builder pattern (1 answer) “borrowed value does not live long enough” seems to blame the wrong thing (2 answers) Why is it legal to borrow a temporary? (3 answers) Why does the compiler tell me to consider using a `let` binding" when I already am? (2 answers) Closed last year . I'm trying to scrape a webpage using the Select crate: let document = Document::from_read(response).unwrap(); for node

How to accept &str, String and &String in a single function?

橙三吉。 提交于 2020-05-25 06:35:18
问题 I want to write a single function, that accepts a &str , a String and a borrowed &String . I've written the following 2 functions: fn accept_str_and_ref_string(value: &str) { println!("value: {}", value); } fn accept_str_and_string<S: Into<String>>(value: S) { let string_value: String = value.into(); println!("string_value: {}", string_value); } fn main() { let str_foo = "foo"; let string_foo = String::from("foo"); accept_str_and_ref_string(str_foo); accept_str_and_ref_string(&string_foo);

Why does “move” in Rust not actually move?

醉酒当歌 提交于 2020-05-25 04:32:06
问题 In the below example: struct Foo { a: [u64; 100000], } fn foo(mut f: Foo) -> Foo { f.a[0] = 99999; f.a[1] = 99999; println!("{:?}", &mut f as *mut Foo); for i in 0..f.a[0] { f.a[i as usize] = 21444; } return f; } fn main(){ let mut f = Foo { a:[0;100000] }; println!("{:?}", &mut f as *mut Foo); f = foo(f); println!("{:?}", &mut f as *mut Foo); } I find that before and after passing into the function foo , the address of f is different. Why does Rust copy such a big struct everywhere but not

How do I tell Cargo to build files other than main.rs?

纵饮孤独 提交于 2020-05-25 04:10:08
问题 Here is my directory structure: lowks@lowkster ~/src/rustlang/gettingrusty $ tree . . ├── Cargo.lock ├── Cargo.toml ├── foo.txt ├── src │ ├── boolean_example.rs │ ├── function_goodbye_world.rs │ ├── listdir.rs │ ├── looping.rs │ ├── main.rs │ ├── pattern_match.rs │ └── write_to_file.rs └── target ├── build ├── deps ├── examples ├── gettingrusty └── native 6 directories, 11 files When I run 'cargo build', it seems to only build main.rs . How should I change Cargo.toml to build the rest of the