summaryrefslogtreecommitdiff
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
parent973e27b243ea7f12b6743894465c67a4a6a87eb2 (diff)
Move rust playground here
Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--rust/chap3/flow.rs50
-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
-rwxr-xr-xrust/chap5/rectanglebin0 -> 3701896 bytes
-rw-r--r--rust/chap5/rectangle.rs54
15 files changed, 225 insertions, 0 deletions
diff --git a/rust/chap3/flow.rs b/rust/chap3/flow.rs
new file mode 100644
index 0000000..36557e5
--- /dev/null
+++ b/rust/chap3/flow.rs
@@ -0,0 +1,50 @@
+
+fn if_me() {
+ let number = 6;
+
+ if number < 5 {
+ println!("{number}");
+ } else {
+ println!("Number too high");
+ }
+
+ let choice = false;
+
+ let b = if choice { 24 } else { 64 };
+
+ println!("Choice is {b}");
+}
+
+fn loop_me() {
+
+ let mut c = 5;
+
+ /*
+ * Rust allow loops to me labeled, and use such labels into
+ * break and continue commands to decide what loop to break/continue
+ */
+ let res = 'loop_label: loop {
+ println!("Again");
+
+ if c == 0 {
+ break 'loop_label 99; // Return a value from the loop to the variable
+ }
+
+ c = c - 1;
+ };
+
+ println!("Value returned by the loop {res}");
+}
+
+fn for_me() {
+ unimplemented!();
+}
+
+fn while_me() {
+ unimplemented!();
+}
+
+fn main() {
+ if_me();
+ loop_me();
+}
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);
+}
diff --git a/rust/chap5/rectangle b/rust/chap5/rectangle
new file mode 100755
index 0000000..1f4142c
--- /dev/null
+++ b/rust/chap5/rectangle
Binary files differ
diff --git a/rust/chap5/rectangle.rs b/rust/chap5/rectangle.rs
new file mode 100644
index 0000000..3b0ab98
--- /dev/null
+++ b/rust/chap5/rectangle.rs
@@ -0,0 +1,54 @@
+#[derive(Debug)]
+struct Rectangle {
+ width: u32,
+ height: u32,
+}
+
+impl Rectangle {
+
+ // First parameter for methods must be named 'self', but the
+ // implementation provides some aliases like:
+ //
+ // self == self: Self
+ // &self == self: &Self
+ //
+ // Note though that 'Self' is just an alias for the type referenced
+ // by the impl{} block. So, here:
+ //
+ // impl Rectangle {}
+ // ^ Self is an alias to Rectangle
+
+ fn can_hold(&self, r: &Rectangle) -> bool {
+ self.width > r.width && self.height > r.height
+ }
+ // Here we can pass just a reference to a struct
+ // Rectangle, no need to transfer ownership
+ fn area(self: &Rectangle) -> u32 {
+ self.width * self.height
+ }
+
+ // Associated functions can be defined without a self parameter
+ // so they function can be namespaced with ::
+ fn square(size: u32) -> Self {
+ Self {
+ width: size,
+ height: size,
+ }
+ }
+}
+
+fn main() {
+ let rect = Rectangle {
+ width: dbg!(5 * 10),
+ height: 3,
+ };
+
+ let sqrt = Rectangle::square(90);
+
+ println!("{rect:#?} Area: {}", rect.area());
+
+ dbg!(&rect);
+ dbg!(&sqrt);
+
+ println!("{}", sqrt.can_hold(&rect));
+}