Skip to content

Instantly share code, notes, and snippets.

@hcyang1012
Created January 25, 2023 16:46
Show Gist options
  • Save hcyang1012/105f3f16a6ed98083097e45f04a63063 to your computer and use it in GitHub Desktop.
Save hcyang1012/105f3f16a6ed98083097e45f04a63063 to your computer and use it in GitHub Desktop.
TRPL/CH04
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);
}
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