Populate
This commit is contained in:
18
src/day1/lifetimes-function-calls.rs
Normal file
18
src/day1/lifetimes-function-calls.rs
Normal file
@ -0,0 +1,18 @@
|
||||
// https://google.github.io/comprehensive-rust/ownership/lifetimes-function-calls.html
|
||||
#[derive(Debug)]
|
||||
struct Point(i32, i32);
|
||||
|
||||
// 'a is a generic parameter, it is inferred by the compiler.
|
||||
// Lifetimes start with ' and 'a is a typical default name.
|
||||
// Read &'a Point as “a borrowed Point which is valid for at least the lifetime a”.
|
||||
// The at least part is important when parameters are in different scopes.
|
||||
fn left_most<'a>(p1: &'a Point, p2: &'a Point) -> &'a Point {
|
||||
if p1.0 < p2.0 { p1 } else { p2 }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let p1: Point = Point(10, 10);
|
||||
let p2: Point = Point(20, 20); // Put into different scope
|
||||
let p3: &Point = left_most(&p1, &p2);
|
||||
println!("left-most point: {:?}", p3);
|
||||
}
|
Reference in New Issue
Block a user