Skip to content

Commit

Permalink
Add copy vs move types
Browse files Browse the repository at this point in the history
  • Loading branch information
simsekgokhan committed Jan 17, 2024
1 parent a8eeff8 commit 9300823
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/add-one-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
//
// Sol:
// Until this issue is solved https://github.com/rust-lang/cargo/issues/8379,
// using this small hack:
//
// using this small hack:
// Make some test modules available for "use-foo" feature
// #usefoo - enable this line when this feature is enabled
// (can be enabled by client's Cargo.toml or compiler flag)
Expand Down
32 changes: 32 additions & 0 deletions src/copy_vs_move.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[test]
fn ex1_ww() {
// a. copy
let x = 5;
let y = x; // !! copied, not moved
println!("x: {}!", x);
// Ok: Since because integers are simple values with a known,
// fixed size, and these two 5 values are pushed onto the stack

// b. move
let s1 = String::from("hello");
let s2 = s1; // !! s1 moved to s2, s1 is invalid at this point
// println!("s1: {}!", s1);
// error[E0382]: borrow of moved value: `s1`
// --> src/main.rs:15:25
// |
// 12 | let s1 = String::from("hello");
// | -- move occurs because `s1` has type `String`,
// which does not implement the `Copy` trait
// 13 | let s2 = s1;
// | -- value moved here
// 14 |
// 15 | println!("s1: {}!", s1);
// | ^^ value borrowed here after move
// |
// help: consider cloning the value if the performance cost is acceptable
// |
// 13 | let s2 = s1.clone();
// | ++++++++

assert_eq!(32, 32);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
mod async_;
mod averaged_collection;
mod cfg;
mod copy_vs_move;
mod ctor_chain;
mod compile_vs_runtime_read_env_and_file;
mod concurrency;
Expand Down
4 changes: 2 additions & 2 deletions src/temp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Template - copy and change this:
//
// #[test]
// #[test]
// fn ex1_ww() {
// assert_eq!(32,32);
// assert_eq!(32, 32);
// }
20 changes: 14 additions & 6 deletions src/traits_default_type_parameter.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::ops::Add;

// Original Add trait from std
pub trait Add_<Rhs = Self> { // <Rhs = Self>: default type parameter
type Output;
fn add(self, rhs: Rhs) -> Self::Output;
}

// 1.a. Using "default type parameter"
// Implementing the Add trait to overload the + operator for Point instances
#[derive(Debug, PartialEq)]
struct Point { x: i32, y: i32 }

// For info: Add trait from std
pub trait Add_<Rhs = Self> { // <Rhs = Self>: default type parameter
type Output;
fn add(self, rhs: Rhs) -> Self::Output;
}

impl Add for Point {
type Output = Point;

Expand All @@ -30,9 +30,17 @@ impl Add for Point {
}

// 1.b. Overriding "default type parameter" also newtype pattern
// https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#default-generic-type-parameters-and-operator-overloading

#[derive(Debug, PartialEq)] struct Millimeters(u32);
#[derive(Debug, PartialEq)] struct Meters(u32);

// For info: Add trait from std
trait Add__<Rhs=Self> {
type Output;
fn add(self, rhs: Rhs) -> Self::Output;
}

impl Add<Meters> for Millimeters {
type Output = Millimeters;
fn add(self, other: Meters) -> Millimeters {
Expand Down

0 comments on commit 9300823

Please sign in to comment.