Taesoo Kim
Taesoo Kim
&str with String&str with &Stringstr with [u8; N]?str with [u8]?&str with &[u8]?The book mentioned the lifetime system being too coarse and throwing compiler errors where it shouldn’t, since it hasn’t been updated and fixed yet. Have you personally experienced this often, and wrote correct code that was counted as incorrect? Should we expect this to happen often?
?Sized or DST)
T) is only referred via a reference (&T)String for str)// in Rust (rustc -C opt-level=3)
fn compute(input: &u32, output: &mut u32) {
if *input > 10 { *output = 1; }
if *input > 5 { *output *= 2; }
}fn compute(input: &u32, output: &mut u32):
if *input > 10 { *output = 1; }
if *input > 5 { *output *= 2; }00000000000044c0 <testing::compute>:
44c0: mov eax,DWORD PTR [rdi]
44c2: cmp eax,0xb
+---44c5: jb 44d4 <testing::compute+0x14>
| 44c7: mov DWORD PTR [rsi],0x1
| 44cd: mov eax,0x2
|+- 44d2: jmp 44dd <testing::compute+0x1d>
+|->44d4: cmp eax,0x6
|+-44d7: jb 44df <testing::compute+0x1f>
|| 44d9: mov eax,DWORD PTR [rsi]
|| 44db: add eax,eax
+|>44dd: mov DWORD PTR [rsi],eax
+>44df: retinput) and #stores (to output)?00000000000044c0 <testing::compute>:
44c0: mov eax,DWORD PTR [rdi]
44c2: cmp eax,0xb
+---44c5: jb 44d4 <testing::compute+0x14>
| 44c7: mov DWORD PTR [rsi],0x1 ; Q. isn't ok to mov 0x2/ret?
| 44cd: mov eax,0x2
|+- 44d2: jmp 44dd <testing::compute+0x1d>
+|->44d4: cmp eax,0x6
|+-44d7: jb 44df <testing::compute+0x1f>
|| 44d9: mov eax,DWORD PTR [rsi]
|| 44db: add eax,eax
+|>44dd: mov DWORD PTR [rsi],eax
+>44df: retx['a]: x’s lifetime is 'a&'a: referring to a variable that lives at least 'a&str but for an outstanding &String!// an instance of S should live as long as s ('a)
// or consider it as a constructor function that accepts
// &'a str and produces S that should live just long as 'a
struct S<'a> {
s: &'a str,
}
// promises to produce &str that lives just long as both params
fn get_longer<'a>(s1: &'a str, s2: &'a str) -> &'a str {
if s1.len() < s2.len() {
s2
} else {
s1
}
}