rust

How to pass Iterator<String> as Iterator<&str>?

左心房为你撑大大i 提交于 2021-02-10 05:22:41
问题 fn my_print<'a>(args: impl Iterator<Item=&'a str>) { for arg in args { println!("=> {}", arg); } } fn main() { let vec = vec!["one".to_string(), "two".to_string()]; my_print(vec.into_iter()); // how to pass vec here? } How do I convert Iterator<T> to Iterator<U> and pass it to another function? 回答1: An even better way would be to write the function in a way such as it doesn't actually care: fn my_print<T: AsRef<str>>(args: impl Iterator<Item = T>) { for arg in args { println!("=> {}", arg.as

Can casting in safe Rust ever lead to a runtime error?

我是研究僧i 提交于 2021-02-10 04:13:50
问题 I'm fiddling a bit with Any and casting just to get a deeper understanding of Rust. From C# I'm used to the fact that casting can lead to runtime exceptions because casting in C# basically means Dear compiler, trust me, I know what I'm doing please cast this into an int32 because I know it will work. However, if you're doing an invalid cast the program will blow up with an Exception at runtime. So I was wondering if casting in (safe) Rust can equally lead to a runtime exception. So, I came up

Representing enum variants with optional data in macro_rules

左心房为你撑大大i 提交于 2021-02-10 00:36:23
问题 I'm trying to create a macro to help with some boilerplate enum code that I've been repetitively writing. I managed to implement a simple enum (i.e. no arguments) relatively easily using a basic macro_rule . e.g. An excerpt: macro_rules! enum_helper { ($type:ident, { $( $name:ident ), * }) => { enum $type { $( $name, )+ } impl FromSql for $type { fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<&type> { // ... the implementation } // ... plus some other internal traits

Representing enum variants with optional data in macro_rules

一笑奈何 提交于 2021-02-10 00:33:48
问题 I'm trying to create a macro to help with some boilerplate enum code that I've been repetitively writing. I managed to implement a simple enum (i.e. no arguments) relatively easily using a basic macro_rule . e.g. An excerpt: macro_rules! enum_helper { ($type:ident, { $( $name:ident ), * }) => { enum $type { $( $name, )+ } impl FromSql for $type { fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<&type> { // ... the implementation } // ... plus some other internal traits

Representing enum variants with optional data in macro_rules

只谈情不闲聊 提交于 2021-02-10 00:26:55
问题 I'm trying to create a macro to help with some boilerplate enum code that I've been repetitively writing. I managed to implement a simple enum (i.e. no arguments) relatively easily using a basic macro_rule . e.g. An excerpt: macro_rules! enum_helper { ($type:ident, { $( $name:ident ), * }) => { enum $type { $( $name, )+ } impl FromSql for $type { fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<&type> { // ... the implementation } // ... plus some other internal traits

Representing enum variants with optional data in macro_rules

做~自己de王妃 提交于 2021-02-10 00:26:18
问题 I'm trying to create a macro to help with some boilerplate enum code that I've been repetitively writing. I managed to implement a simple enum (i.e. no arguments) relatively easily using a basic macro_rule . e.g. An excerpt: macro_rules! enum_helper { ($type:ident, { $( $name:ident ), * }) => { enum $type { $( $name, )+ } impl FromSql for $type { fn from_sql<R: Read>(_: &Type, raw: &mut R, _: &SessionInfo) -> Result<&type> { // ... the implementation } // ... plus some other internal traits

Share Arc between closures

南楼画角 提交于 2021-02-09 17:59:07
问题 I'm trying to write a simple tcp server which would read and broadcast messages. I'm using Tokio, but I think it's more of a general Rust question. I have an Arc with a shared state: let state = Arc::new(Mutex::new(Shared::new(server_tx))); Later I want to spawn 2 threads which would use a reference to that state: let server = listener.incoming().for_each(move |socket| { // error[E0382]: capture of moved value: `state` process(socket, state.clone()); Ok(()) }).map_err(|err| { println!("accept

Share Arc between closures

妖精的绣舞 提交于 2021-02-09 17:58:34
问题 I'm trying to write a simple tcp server which would read and broadcast messages. I'm using Tokio, but I think it's more of a general Rust question. I have an Arc with a shared state: let state = Arc::new(Mutex::new(Shared::new(server_tx))); Later I want to spawn 2 threads which would use a reference to that state: let server = listener.incoming().for_each(move |socket| { // error[E0382]: capture of moved value: `state` process(socket, state.clone()); Ok(()) }).map_err(|err| { println!("accept

What is RUST_BACKTRACE supposed to tell me?

◇◆丶佛笑我妖孽 提交于 2021-02-09 12:50:35
问题 My program is panicking so I followed its advice to run RUST_BACKTRACE=1 and I get this (just a little snippet). 1: 0x800c05b5 - std::sys::imp::backtrace::tracing::imp::write::hf33ae72d0baa11ed at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42 2: 0x800c22ed - std::panicking::default_hook::{{closure}}::h59672b733cc6a455 at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:351 If the program

What is RUST_BACKTRACE supposed to tell me?

时间秒杀一切 提交于 2021-02-09 12:48:55
问题 My program is panicking so I followed its advice to run RUST_BACKTRACE=1 and I get this (just a little snippet). 1: 0x800c05b5 - std::sys::imp::backtrace::tracing::imp::write::hf33ae72d0baa11ed at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42 2: 0x800c22ed - std::panicking::default_hook::{{closure}}::h59672b733cc6a455 at /buildslave/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libstd/panicking.rs:351 If the program