From 93b1c04a218858ecc59b6b8929103695b7b8c2a0 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:30:14 +0200 Subject: Move rust playground here Signed-off-by: Carlos Maiolino --- rust/chap4/borrow | Bin 0 -> 3707760 bytes rust/chap4/borrow.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ rust/chap4/plus_one | Bin 0 -> 3694552 bytes rust/chap4/plus_one.rs | 10 ++++++++++ rust/chap4/simple_ref | Bin 0 -> 3699056 bytes rust/chap4/simple_ref.rs | 16 ++++++++++++++++ rust/chap4/slice_me | Bin 0 -> 3704096 bytes rust/chap4/slice_me.rs | 26 ++++++++++++++++++++++++++ rust/chap4/test | Bin 0 -> 3693928 bytes rust/chap4/test.rs | 14 ++++++++++++++ rust/chap4/vecs | Bin 0 -> 3702920 bytes rust/chap4/vecs.rs | 11 +++++++++++ 12 files changed, 121 insertions(+) create mode 100755 rust/chap4/borrow create mode 100644 rust/chap4/borrow.rs create mode 100755 rust/chap4/plus_one create mode 100644 rust/chap4/plus_one.rs create mode 100755 rust/chap4/simple_ref create mode 100644 rust/chap4/simple_ref.rs create mode 100755 rust/chap4/slice_me create mode 100644 rust/chap4/slice_me.rs create mode 100755 rust/chap4/test create mode 100644 rust/chap4/test.rs create mode 100755 rust/chap4/vecs create mode 100644 rust/chap4/vecs.rs (limited to 'rust/chap4') diff --git a/rust/chap4/borrow b/rust/chap4/borrow new file mode 100755 index 0000000..2c28095 Binary files /dev/null and b/rust/chap4/borrow differ diff --git a/rust/chap4/borrow.rs b/rust/chap4/borrow.rs new file mode 100644 index 0000000..36f1e9a --- /dev/null +++ b/rust/chap4/borrow.rs @@ -0,0 +1,44 @@ + +/* + * g1 and g2 here are "references to the m1/m2 variables, + * I believe they do not point directly to the data address, + * but to the variables addresses. + * + * This makes greet to 'borrow' the reference to the strings, + * and once the greet() stackframe is gone, m1 and m2 are not + * freed, just g1 and g2. And since they don't own anything, + * nothing in the heap is freed. + */ +fn greet(g1: &String, g2: &String) { + println!("{} {}", *g1, *g2); +} + +fn crazy_ref() { + + let x: Box = Box::new(-1); + let x_abs1 = i32::abs(*x); + let x_abs2 = x.abs(); + assert_eq!(x_abs1, x_abs2); + + let r: &Box = &x; + let r_abs1 = i32::abs(**r); + let r_abs2 = r.abs(); + assert_eq!(r_abs1, r_abs2); + + let s = String::from("Howdy"); + let s_len1 = str::len(&s); + let s_len2 = s.len(); + assert_eq!(s_len1, s_len2); +} + +fn main() { + let m1 = String::from("Howdy"); + let m2 = String::from("partner"); + + /* + * Pass a reference to m1 and m2 to the function parameters. + */ + greet(&m1, &m2); + crazy_ref(); + +} diff --git a/rust/chap4/plus_one b/rust/chap4/plus_one new file mode 100755 index 0000000..df7de06 Binary files /dev/null and b/rust/chap4/plus_one differ diff --git a/rust/chap4/plus_one.rs b/rust/chap4/plus_one.rs new file mode 100644 index 0000000..e28f415 --- /dev/null +++ b/rust/chap4/plus_one.rs @@ -0,0 +1,10 @@ +fn plus_one(i: i32) -> i32 { + i + 1 +} + +fn main() { + let i = 3; + + println!("Num {}", plus_one(i)); + +} diff --git a/rust/chap4/simple_ref b/rust/chap4/simple_ref new file mode 100755 index 0000000..e519673 Binary files /dev/null and b/rust/chap4/simple_ref differ diff --git a/rust/chap4/simple_ref.rs b/rust/chap4/simple_ref.rs new file mode 100644 index 0000000..968c025 --- /dev/null +++ b/rust/chap4/simple_ref.rs @@ -0,0 +1,16 @@ +fn main() { + let mut s = String::from("howdy"); + + change_me(&mut s); + + let size = get_str_size(&s); + println!("Size of s is {size}"); +} + +fn get_str_size(s: &String) -> usize { + return s.len(); +} + +fn change_me(s: &mut String) { + s.push_str(", brilha no curintia"); +} diff --git a/rust/chap4/slice_me b/rust/chap4/slice_me new file mode 100755 index 0000000..d0f1d77 Binary files /dev/null and b/rust/chap4/slice_me differ diff --git a/rust/chap4/slice_me.rs b/rust/chap4/slice_me.rs new file mode 100644 index 0000000..da201a1 --- /dev/null +++ b/rust/chap4/slice_me.rs @@ -0,0 +1,26 @@ + +/* Return the index of the first space or s.len()*/ +fn get_word_index(s: &str) -> usize { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return i; + } + } + + return s.len(); +} + +fn first_word(s: &str) -> &str { + let idx = get_word_index(s); + + return &s[..idx]; +} + +fn main() { + let s = String::from("corno manso ronaldo curintiano"); + let slice = first_word(&s); + + println!("{slice}"); +} diff --git a/rust/chap4/test b/rust/chap4/test new file mode 100755 index 0000000..c45dc0c Binary files /dev/null and b/rust/chap4/test differ diff --git a/rust/chap4/test.rs b/rust/chap4/test.rs new file mode 100644 index 0000000..eabcaeb --- /dev/null +++ b/rust/chap4/test.rs @@ -0,0 +1,14 @@ +fn read(y: bool) { + if y { + println!("Y is true"); + } + +} + +fn main() { + + read(t); + let t: bool = true; + + return; +} diff --git a/rust/chap4/vecs b/rust/chap4/vecs new file mode 100755 index 0000000..470bd01 Binary files /dev/null and b/rust/chap4/vecs differ diff --git a/rust/chap4/vecs.rs b/rust/chap4/vecs.rs new file mode 100644 index 0000000..b0a76be --- /dev/null +++ b/rust/chap4/vecs.rs @@ -0,0 +1,11 @@ +fn main() { + let mut v: Vec = vec![1, 2, 3]; + + /* Push an item calling method direct from Vec definition */ + Vec::push(&mut v, 4); + + /* Push an item calling the method via its object */ + v.push(5); + + println!("{:?}", v); +} -- cgit v1.2.3