From f933889220b8cd0e8907536f243a172ea6d84d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C5=9Eim=C5=9Fek?= Date: Tue, 26 Dec 2023 09:39:35 +0300 Subject: [PATCH] Adding return impl usage --- src/let_else.rs | 3 ++- src/lib.rs | 1 + src/match_.rs | 10 +++++++- src/multi_threading_par_iter.rs | 2 +- src/serde_json.rs | 2 +- src/traits_default_type_parameter.rs | 2 +- src/traits_return_impl.rs | 35 ++++++++++++++++++++++++++++ 7 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/traits_return_impl.rs diff --git a/src/let_else.rs b/src/let_else.rs index 4c6fd19..23d92fb 100644 --- a/src/let_else.rs +++ b/src/let_else.rs @@ -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); } diff --git a/src/lib.rs b/src/lib.rs index 764bba4..05b93ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/match_.rs b/src/match_.rs index 10fa520..c02d739 100644 --- a/src/match_.rs +++ b/src/match_.rs @@ -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 can be have value or not +// pub enum Option { +// /// 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); diff --git a/src/multi_threading_par_iter.rs b/src/multi_threading_par_iter.rs index a4cdf24..f944fb2 100644 --- a/src/multi_threading_par_iter.rs +++ b/src/multi_threading_par_iter.rs @@ -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 diff --git a/src/serde_json.rs b/src/serde_json.rs index 8f6c66c..7c50d4f 100644 --- a/src/serde_json.rs +++ b/src/serde_json.rs @@ -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, diff --git a/src/traits_default_type_parameter.rs b/src/traits_default_type_parameter.rs index 3402e9e..7a823cd 100644 --- a/src/traits_default_type_parameter.rs +++ b/src/traits_default_type_parameter.rs @@ -1,6 +1,6 @@ use std::ops::Add; -// Original Add trait +// Original Add trait from std pub trait Add_ { // : default type parameter type Output; fn add(self, rhs: Rhs) -> Self::Output; diff --git a/src/traits_return_impl.rs b/src/traits_return_impl.rs new file mode 100644 index 0000000..a6a8475 --- /dev/null +++ b/src/traits_return_impl.rs @@ -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 { + Box::new(5) as Box +} + +//// Real word usage: +// before +fn get_closure() -> Box 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 +} \ No newline at end of file