Move tons of stuff into oldstuff/
This commit is contained in:
6
oldstuff/rustbook/ownership/Cargo.toml
Normal file
6
oldstuff/rustbook/ownership/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "ownership"
|
||||
version = "0.1.0"
|
||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
||||
|
||||
[dependencies]
|
26
oldstuff/rustbook/ownership/src/main.rs
Normal file
26
oldstuff/rustbook/ownership/src/main.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
fn main() {
|
||||
let s = String::from("hello world");
|
||||
|
||||
let word = first_word(&s[..]);
|
||||
println!("word={}", word);
|
||||
|
||||
let l = "hello world";
|
||||
|
||||
let word = first_word(&l[..]);
|
||||
println!("word={}", word);
|
||||
|
||||
let word = first_word(l);
|
||||
println!("word={}", word);
|
||||
}
|
||||
|
||||
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[..]
|
||||
}
|
Reference in New Issue
Block a user