Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(nll)]
- fn strlen(buf: &[u8]) -> Option<usize> {
- buf.iter().position(|&c| c == b'\0')
- }
- fn strcat(buf: &mut Option<&mut [u8]>, s: &[u8]) {
- if let Some(ref mut b) = buf {
- let l1 = strlen(b).expect("no terminator on buf");
- let l2 = strlen(s).expect("no terminator on s");
- if l1 + l2 + 1 > b.len() {
- *buf = None;
- } else {
- for i in 0..l2 {
- b[l1 + i] = s[i];
- }
- b[l1 + l2] = b'\0';
- }
- }
- }
- fn main() {
- let mut buf = [0u8; 10];
- let mut string = Some(&mut buf[..]);
- strcat(&mut string, b"ciao\0"); //non c'e' il \0 di default, quindi ho scritto anche \0
- strcat(&mut string, b"vb\0");
- //NON posso accedere a buf perche' e' borrowed da string
- //questo non compilerebbe
- //println!("{}",buf[0]);
- //NON posso accedere a string perche' potrebbe essere None
- //questo non compilerebbe
- //println!("{}",string[0]);
- match string {
- Some(ref s) => println!("{:?}", s),
- None => println!("you did something bad"),
- }
- strcat(&mut string, b"vbbbbbbbbb\0");
- match string {
- Some(ref s) => println!("{:?}", s),
- None => println!("you did something bad"),
- }
- }
Add Comment
Please, Sign In to add comment