rust

How do I conditionally return different types of futures?

[亡魂溺海] 提交于 2021-02-10 17:25:21
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn't compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8 |

How do I conditionally return different types of futures?

元气小坏坏 提交于 2021-02-10 17:25:09
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn't compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8 |

How do I conditionally return different types of futures?

人盡茶涼 提交于 2021-02-10 17:24:48
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn't compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8 |

How do I conditionally return different types of futures?

给你一囗甜甜゛ 提交于 2021-02-10 17:24:27
问题 I have a method that, depending on a predicate, will return one future or another. In other words, an if-else expression that returns a future: extern crate futures; // 0.1.23 use futures::{future, Future}; fn f() -> impl Future<Item = usize, Error = ()> { if 1 > 0 { future::ok(2).map(|x| x) } else { future::ok(10).and_then(|x| future::ok(x + 2)) } } This doesn't compile: error[E0308]: if and else have incompatible types --> src/lib.rs:6:5 | 6 | / if 1 > 0 { 7 | | future::ok(2).map(|x| x) 8 |

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

北战南征 提交于 2021-02-10 17:08:14
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

偶尔善良 提交于 2021-02-10 17:05:19
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

喜你入骨 提交于 2021-02-10 17:04:35
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

What are non-lexical lifetimes?

不羁岁月 提交于 2021-02-10 16:42:34
问题 Rust has an RFC related to non-lexical lifetimes which has been approved to be implemented in the language for a long time. Recently, Rust's support of this feature has improved a lot and is considered complete. My question is: what exactly is a non-lexical lifetime? 回答1: It's easiest to understand what non-lexical lifetimes are by understanding what lexical lifetimes are. In versions of Rust before non-lexical lifetimes are present, this code will fail: fn main() { let mut scores = vec![1, 2

Is there a shorthand form of the match in Rust?

不想你离开。 提交于 2021-02-10 15:16:54
问题 Many function calls in Rust return the type std::result::Result which is an enum. There are advantages of having such a return type, but writing a matcher looks to be tedious for a small task. For example, I was trying to find the time a certain portion of my code takes. I tried SystemTime::now() coupled with duration() : let now = SystemTime::now(); let result = cvar .wait_timeout(started, Duration::from_millis(20000)) .unwrap(); started = result.0; if *started == false { *started = true; }

Is there a shorthand form of the match in Rust?

夙愿已清 提交于 2021-02-10 15:14:13
问题 Many function calls in Rust return the type std::result::Result which is an enum. There are advantages of having such a return type, but writing a matcher looks to be tedious for a small task. For example, I was trying to find the time a certain portion of my code takes. I tried SystemTime::now() coupled with duration() : let now = SystemTime::now(); let result = cvar .wait_timeout(started, Duration::from_millis(20000)) .unwrap(); started = result.0; if *started == false { *started = true; }