Created
January 25, 2023 16:46
-
-
Save hcyang1012/105f3f16a6ed98083097e45f04a63063 to your computer and use it in GitHub Desktop.
TRPL/CH04
This file contains 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 main() { | |
let s = String::from("hello"); | |
takes_ownership(s); | |
let x = 5; | |
makes_copy(x); | |
} | |
fn takes_ownership(some_string: String){ | |
println!("{}", some_string); | |
} | |
fn makes_copy(some_integer: i32){ | |
println!("{}", some_integer); | |
} |
This file contains 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 main() { | |
let my_string = String::from("Hello world"); | |
println!("{}", first_word(&my_string[..])); | |
} | |
fn first_word(s: &str) -> &str { | |
let bytes = s.as_bytes(); | |
for (i, &item) in bytes.iter().enumerate() { | |
if item == b' '{ | |
return &s[0..i]; | |
} | |
} | |
&s[..] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment