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/chap3/flow.rs | 50 +++++++++++++++++++++++++++++++++++++++++++ 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 ++++++++++ rust/chap5/rectangle | Bin 0 -> 3701896 bytes rust/chap5/rectangle.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 225 insertions(+) create mode 100644 rust/chap3/flow.rs 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 create mode 100755 rust/chap5/rectangle create mode 100644 rust/chap5/rectangle.rs 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 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); +} diff --git a/rust/chap5/rectangle b/rust/chap5/rectangle new file mode 100755 index 0000000..1f4142c Binary files /dev/null and b/rust/chap5/rectangle 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)); +} -- cgit v1.2.3