summaryrefslogtreecommitdiff
path: root/rust/chap4
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-09-06 09:30:14 +0200
committerCarlos Maiolino <[email protected]>2025-09-06 09:30:14 +0200
commit93b1c04a218858ecc59b6b8929103695b7b8c2a0 (patch)
tree7ae24ff4b2ef06c8d961f2c908ba8511e1fc995b /rust/chap4
parent973e27b243ea7f12b6743894465c67a4a6a87eb2 (diff)
Move rust playground here
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'rust/chap4')
-rwxr-xr-xrust/chap4/borrowbin0 -> 3707760 bytes
-rw-r--r--rust/chap4/borrow.rs44
-rwxr-xr-xrust/chap4/plus_onebin0 -> 3694552 bytes
-rw-r--r--rust/chap4/plus_one.rs10
-rwxr-xr-xrust/chap4/simple_refbin0 -> 3699056 bytes
-rw-r--r--rust/chap4/simple_ref.rs16
-rwxr-xr-xrust/chap4/slice_mebin0 -> 3704096 bytes
-rw-r--r--rust/chap4/slice_me.rs26
-rwxr-xr-xrust/chap4/testbin0 -> 3693928 bytes
-rw-r--r--rust/chap4/test.rs14
-rwxr-xr-xrust/chap4/vecsbin0 -> 3702920 bytes
-rw-r--r--rust/chap4/vecs.rs11
12 files changed, 121 insertions, 0 deletions
diff --git a/rust/chap4/borrow b/rust/chap4/borrow
new file mode 100755
index 0000000..2c28095
--- /dev/null
+++ b/rust/chap4/borrow
Binary files 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<i32> = Box::new(-1);
+ let x_abs1 = i32::abs(*x);
+ let x_abs2 = x.abs();
+ assert_eq!(x_abs1, x_abs2);
+
+ let r: &Box<i32> = &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
--- /dev/null
+++ b/rust/chap4/plus_one
Binary files 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
--- /dev/null
+++ b/rust/chap4/simple_ref
Binary files 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
--- /dev/null
+++ b/rust/chap4/slice_me
Binary files 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
--- /dev/null
+++ b/rust/chap4/test
Binary files 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
--- /dev/null
+++ b/rust/chap4/vecs
Binary files 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<i32> = 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);
+}