diff options
Diffstat (limited to 'rust/chap4')
| -rwxr-xr-x | rust/chap4/borrow | bin | 0 -> 3707760 bytes | |||
| -rw-r--r-- | rust/chap4/borrow.rs | 44 | ||||
| -rwxr-xr-x | rust/chap4/plus_one | bin | 0 -> 3694552 bytes | |||
| -rw-r--r-- | rust/chap4/plus_one.rs | 10 | ||||
| -rwxr-xr-x | rust/chap4/simple_ref | bin | 0 -> 3699056 bytes | |||
| -rw-r--r-- | rust/chap4/simple_ref.rs | 16 | ||||
| -rwxr-xr-x | rust/chap4/slice_me | bin | 0 -> 3704096 bytes | |||
| -rw-r--r-- | rust/chap4/slice_me.rs | 26 | ||||
| -rwxr-xr-x | rust/chap4/test | bin | 0 -> 3693928 bytes | |||
| -rw-r--r-- | rust/chap4/test.rs | 14 | ||||
| -rwxr-xr-x | rust/chap4/vecs | bin | 0 -> 3702920 bytes | |||
| -rw-r--r-- | rust/chap4/vecs.rs | 11 |
12 files changed, 121 insertions, 0 deletions
diff --git a/rust/chap4/borrow b/rust/chap4/borrow Binary files differnew file mode 100755 index 0000000..2c28095 --- /dev/null +++ b/rust/chap4/borrow 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 Binary files differnew file mode 100755 index 0000000..df7de06 --- /dev/null +++ b/rust/chap4/plus_one 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 Binary files differnew file mode 100755 index 0000000..e519673 --- /dev/null +++ b/rust/chap4/simple_ref 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 Binary files differnew file mode 100755 index 0000000..d0f1d77 --- /dev/null +++ b/rust/chap4/slice_me 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 Binary files differnew file mode 100755 index 0000000..c45dc0c --- /dev/null +++ b/rust/chap4/test 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 Binary files differnew file mode 100755 index 0000000..470bd01 --- /dev/null +++ b/rust/chap4/vecs 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); +} |
