commit
This commit is contained in:
parent
e1246d346d
commit
2f6caa57f0
6 changed files with 72 additions and 0 deletions
BIN
ch10/extract_fn
Executable file
BIN
ch10/extract_fn
Executable file
Binary file not shown.
21
ch10/extract_fn.rs
Normal file
21
ch10/extract_fn.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
fn largest(list: &[i32]) -> &i32 {
|
||||||
|
let mut largest = &list[0];
|
||||||
|
|
||||||
|
for num in list {
|
||||||
|
if num > largest {
|
||||||
|
largest = num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
largest
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let number_list = vec![30, 52, 12, 69, 23, 50];
|
||||||
|
let large = largest(&number_list);
|
||||||
|
println!("Largest: {large}");
|
||||||
|
|
||||||
|
let number_list = vec![130, 52, 12, 69, 23, 50];
|
||||||
|
let large = largest(&number_list);
|
||||||
|
println!("Largest: {large}");
|
||||||
|
}
|
||||||
BIN
ch10/lifetime_hypotheses
Executable file
BIN
ch10/lifetime_hypotheses
Executable file
Binary file not shown.
22
ch10/lifetime_hypotheses.rs
Normal file
22
ch10/lifetime_hypotheses.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||||
|
if x.len() > y.len() { x } else { y }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn brap<'b>(x: &'b str, y: &str) -> &'b str {
|
||||||
|
if x.len() > y.len() { x } else { "henkieeeeee" }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let stronk = String::from("stronkman");
|
||||||
|
|
||||||
|
let ln;
|
||||||
|
{
|
||||||
|
let stonks = "stonks";
|
||||||
|
let mut inp = String::new();
|
||||||
|
io::stdin().read_line(&mut inp).expect("Please input a name");
|
||||||
|
ln = longest(&inp, brap(stronk.as_str(), stonks));
|
||||||
|
}
|
||||||
|
println!("The longest of them all: {}", ln);
|
||||||
|
}
|
||||||
BIN
ch10/trait_blanket_implementation
Executable file
BIN
ch10/trait_blanket_implementation
Executable file
Binary file not shown.
29
ch10/trait_blanket_implementation.rs
Normal file
29
ch10/trait_blanket_implementation.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
trait Named {
|
||||||
|
fn say_name(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
trait Bold {
|
||||||
|
fn write_bold(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Named> Bold for T {
|
||||||
|
fn write_bold(&self) {
|
||||||
|
println!("!bold {}", self.say_name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Named for Person {
|
||||||
|
fn say_name(&self) -> String {
|
||||||
|
self.name.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let p = Person{name: String::from("Jasper")};
|
||||||
|
|
||||||
|
p.write_bold();
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue