Skip to content

Commit

Permalink
Adding return impl usage
Browse files Browse the repository at this point in the history
  • Loading branch information
simsekgokhan committed Dec 26, 2023
1 parent 7bd3c6f commit f933889
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ fn process_str(s: &str) -> u64 {
return 42
}
let count = res.unwrap();

// ... do_something_with(count)
count + 1
}

#[test]
#[test]
fn ex1_simple_works() {
assert_eq!(process_str("3"), 4);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod trait_object_vs_struct_obj;
mod traits;
mod traits_associated_type_vs_generics;
mod traits_default_type_parameter;
mod traits_return_impl; // todo: incomplete
mod traits_same_name_fn_call;
mod traits_supertraits;
mod utils;
10 changes: 9 additions & 1 deletion src/match_.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/// 1. Refutability
// Refutable: able to be proven false
// Irrefutable: int x = 42; // x always have value, never false
// Refutable: able to be proven false
// e.g. Option<T> can be have value or not
// pub enum Option<T> {
// /// No value.
// None,
// /// Some value of type `T`.
// Some(#[stable(feature = "rust1", since = "1.0.0")] T),
// }

#[test] fn ex_1() {
let res = Some(42);
Expand Down
2 changes: 1 addition & 1 deletion src/multi_threading_par_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn single_thread_exec(arr: &mut [i32]) {

assert_eq!(32,32);
}
//// Lesson: Parallel exec. is more meaningful when we have bigger arrays
//// Important: Parallel exec. is more meaningful when we have bigger arrays/tasks
//// StdOut:
// (1) Arr size: 5_000
// (a) Parallel exec: 636 us
Expand Down
2 changes: 1 addition & 1 deletion src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde_json::to_string as to_json;
use serde_derive::{Serialize};

// Instructs the serde_derive crate to write the necessary code to carry out
// the conversion from an in-memory City to on-disk City
// the conversion from an in-memory City obj. to on-disk City obj.
#[derive(Serialize)]
struct City {
name: String,
Expand Down
2 changes: 1 addition & 1 deletion src/traits_default_type_parameter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Add;

// Original Add trait
// Original Add trait from std
pub trait Add_<Rhs = Self> { // <Rhs = Self>: default type parameter
type Output;
fn add(self, rhs: Rhs) -> Self::Output;
Expand Down
35 changes: 35 additions & 0 deletions src/traits_return_impl.rs
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
}

0 comments on commit f933889

Please sign in to comment.