Lifetimes in Data Structures
https://google.github.io/comprehensive-rust/ownership/lifetimes-data-structures.html when possible, data structures ought to own their data directly, rather than borrowing a reference.
This commit is contained in:
parent
d2bc5d2981
commit
95f52e96d3
26
src/main.rs
26
src/main.rs
|
@ -1,22 +1,16 @@
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Point(i32, i32);
|
struct Highlight<'doc>(&'doc str);
|
||||||
|
|
||||||
fn left_most<'a>(p1: &'a Point, p2: &'a Point) -> &'a Point {
|
#[allow(dead_code)]
|
||||||
if p1.0 < p2.0 {
|
fn erase(text: String) {
|
||||||
p1
|
println!("Bye {text}!");
|
||||||
} else {
|
|
||||||
p2
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let p1: Point = Point(10, 10);
|
let text = String::from("The quick brown fox jumps over the lazy dog.");
|
||||||
let p2: Point = Point(20, 20);
|
let fox = Highlight(&text[4..19]);
|
||||||
{
|
let dog = Highlight(&text[35..43]);
|
||||||
let p3: &Point = left_most(&p1, &p2);
|
//erase(text);
|
||||||
|
println!("{fox:?}");
|
||||||
println!("left_most: {:?}", p3);
|
println!("{dog:?}");
|
||||||
println!("p3: {:p}", &p3);
|
|
||||||
}
|
|
||||||
println!("p1: {:p}", &p1);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"rustc_fingerprint":16320356917333427825,"outputs":{"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/vbatts/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""},"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.66.1 (90743e729 2023-01-10)\nbinary: rustc\ncommit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6\ncommit-date: 2023-01-10\nhost: x86_64-unknown-linux-gnu\nrelease: 1.66.1\nLLVM version: 15.0.2\n","stderr":""}},"successes":{}}
|
{"rustc_fingerprint":16320356917333427825,"outputs":{"15697416045686424142":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.66.1 (90743e729 2023-01-10)\nbinary: rustc\ncommit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6\ncommit-date: 2023-01-10\nhost: x86_64-unknown-linux-gnu\nrelease: 1.66.1\nLLVM version: 15.0.2\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/vbatts/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"unknown\"\nunix\n","stderr":""}},"successes":{}}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue