This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn the_longest<'a>(s1: &'a str, s2: &'a str) -> &'a str { | |
| if s1.len() > s2.len() { s1 } else { s2 } | |
| } | |
| fn main() { | |
| let s1 = String::from("Python"); | |
| // explicitly borrowing to ensure that | |
| // the borrow lasts longer than s2 exists | |
| let s1_b = &s1; | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn search<'a, 'b>(needle: &'a str, haystack: &'b str) -> Option<&'b str> { | |
| // imagine some clever algorithm here | |
| // that returns a slice of the original string | |
| let len = needle.len(); | |
| if haystack.chars().nth(0) == needle.chars().nth(0) { | |
| Some(&haystack[..len]) | |
| } else if haystack.chars().nth(1) == needle.chars().nth(0) { | |
| Some(&haystack[1..len+1]) | |
| } else { | |
| None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn middle_name<'a>(full_name: &'a str) -> &'a str { | |
| full_name.split_whitespace().nth(1).unwrap() | |
| } | |
| fn main() { | |
| let name = String::from("Harry James Potter"); | |
| let res = middle_name(&name); | |
| assert_eq!(res, "James"); | |
| // won't compile: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| const char *last_name(const char *full_name) | |
| { | |
| return strrchr(full_name, ' ') + 1; | |
| } | |
| int main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct A { | |
| int x; | |
| }; | |
| struct B: A { | |
| int y; | |
| }; | |
| struct C: B { | |
| int z; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // this does not compile | |
| fn middle_name(full_name: &str) -> &str { | |
| full_name.split_whitespace().nth(1).unwrap() | |
| } | |
| fn main() { | |
| let res; | |
| { | |
| let name = String::from("Harry James Potter"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn middle_name(full_name: &str) -> &str { | |
| full_name.split_whitespace().nth(1).unwrap() | |
| } | |
| fn main() { | |
| let name = String::from("Harry James Potter"); | |
| let res = middle_name(&name); | |
| assert_eq!(res, "James"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // takes v by (immutable) reference | |
| fn count_occurences(v: &Vec<i32>, val: i32) -> usize { | |
| v.into_iter().filter(|&&x| x == val).count() | |
| } | |
| fn main() { | |
| let v = vec![2, 9, 3, 1, 3, 2, 5, 5, 2]; | |
| // borrowing v for the iteration | |
| for &item in &v { | |
| // the first borrow is still active |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // without borrowing | |
| fn print_sum1(v: Vec<i32>) -> Vec<i32> { | |
| println!("{}", v[0] + v[1]); | |
| // returning v as a means of transferring ownership back | |
| // by the way, there's no need to use "return" if it's the last line | |
| // because Rust is expression-based | |
| v | |
| } | |
| // with borrowing, explicit references |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn print_sum(a: i32, b: i32) { | |
| println!("{}", a + b); | |
| // the copied a and b are dropped and deallocated here | |
| } | |
| fn main() { | |
| let a = 35; | |
| let b = 42; | |
| // copy the values and transfer | |
| // ownership over the copies to print_sum: |