finally finished slice...
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
fn first_word(s: &String) -> usize {
|
||||
fn first_word_int(s: &str) -> usize {
|
||||
let b = s.as_bytes();
|
||||
for (i, &val) in b.iter().enumerate() {
|
||||
if val == b' ' {
|
||||
@ -7,7 +7,23 @@ fn first_word(s: &String) -> usize {
|
||||
}
|
||||
s.len()
|
||||
}
|
||||
fn main() {
|
||||
println!("{}", first_word(&String::from("sadf sdfsd fesf")));
|
||||
|
||||
fn first_word(s: &str) -> &str {
|
||||
let b = s.as_bytes();
|
||||
for (i, &val) in b.iter().enumerate() {
|
||||
if val == b' ' {
|
||||
return &s[..i];
|
||||
}
|
||||
}
|
||||
&s[..]
|
||||
}
|
||||
fn main() {
|
||||
let test_string = "oh my gerd";
|
||||
let string = String::from("sdf sdf esv gs ");
|
||||
let slice = &string[0..first_word_int(&string)];
|
||||
println!("here is the first word: _{}_", slice);
|
||||
|
||||
println!("here is the first word: _{}_", first_word(&string));
|
||||
println!("here is the first word: _{}_", first_word(test_string));
|
||||
println!("{}", test_string);
|
||||
}
|
||||
|
Reference in New Issue
Block a user