-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bd3c6f
commit f933889
Showing
7 changed files
with
50 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
// https://blog.rust-lang.org/2018/05/10/Rust-1.26.html#impl-trait | ||
|
||
trait Trait { } | ||
impl Trait for i32 { } | ||
impl Trait for f32 { } | ||
|
||
// Use this | ||
// Note: This doesn't create a trait object, it's like we had written -> i32, | ||
// but instead, we're only mentioning the part about Trait. | ||
// We get static dispatch, but we can hide the real type like this. | ||
fn foo() -> impl Trait { | ||
5 | ||
} | ||
// instead of | ||
fn foo_() -> Box<dyn Trait> { | ||
Box::new(5) as Box<dyn Trait> | ||
} | ||
|
||
//// Real word usage: | ||
// before | ||
fn get_closure() -> Box<dyn Fn(i32) -> i32> { | ||
Box::new(|x| x + 1) | ||
} | ||
// after | ||
fn get_closure_() -> impl Fn(i32) -> i32 { | ||
|x| x + 1 | ||
} | ||
|
||
#[test] | ||
fn ex1() { | ||
let xx = foo(); | ||
let yy = foo_(); | ||
// assert_eq!(xx, 5); // todo | ||
} |