match items.iter().position(|y| *y == x) { Some(i) => println!("Found {} at position {}.", x, i), None => println!("There is no {} in the list.", x), } }
leti = items.iter().position(|y| *y == x).map_or(-1, |n| n asi32);
println!("{} => {}", x, i) }
{ letx = 13;
leti = items.iter().position(|y| *y == x).map_or(-1, |n| n asi32);
println!("{} => {}", x, i) } }
1 2
12 => 2 13 => -1
223. for else loop
Loop through list items checking a condition. Do something else if no matches are found. A typical use case is looping through a series of containers looking for one that matches a condition. If found, an item is inserted; otherwise, a new container is created. These are mostly used as an inner nested loop, and in a location where refactoring inner logic into a separate function reduces clarity.
Go does not have optional arguments, but to some extend, they can be mimicked with a variadic parameter. x is a variadic parameter, which must be the last parameter for the function f. Strictly speaking, x is a list of integers, which might have more than one element. These additional elements are ignored.
1 2
Not present Present 1
1 2 3 4 5 6
fnf(x: Option<()>) { match x { Some(x) => println!("Present {}", x), None => println!("Not present"), } }
Create new list y containing the same elements as list x. Subsequent modifications of y must not affect x (except for the contents referenced by the elements themselves if they contain pointers).
funcmain() { a, b := []byte("Hello"), []byte("world")
c := make([]byte, len(a)) for i := range a { c[i] = a[i] ^ b[i] }
fmt.Printf("a is %08b\n", a) fmt.Printf("b is %08b\n", b) fmt.Printf("c is %08b\n", c) fmt.Println("c ==", c) fmt.Printf("c as string would be %q\n", string(c)) }
1 2 3 4 5
a is [0100100001100101011011000110110001101111] b is [0111011101101111011100100110110001100100] c is [0011111100001010000111100000000000001011] c == [631030011] c as string would be "?\n\x1e\x00\v"
funcmain() { var a, b T copy(a[:], "Hello") copy(b[:], "world")
var c T for i := range a { c[i] = a[i] ^ b[i] }
fmt.Printf("a is %08b\n", a) fmt.Printf("b is %08b\n", b) fmt.Printf("c is %08b\n", c) fmt.Println("c ==", c) fmt.Printf("c as string would be %q\n", string(c[:])) }
1 2 3 4 5
a is [0100100001100101011011000110110001101111] b is [0111011101101111011100100110110001100100] c is [0011111100001010000111100000000000001011] c == [631030011] c as string would be "?\n\x1e\x00\v"
1 2 3 4 5 6 7 8
fnmain() { leta: &[u8] = "Hello".as_bytes(); letb: &[u8] = "world".as_bytes();
letc: Vec<_> = a.iter().zip(b).map(|(x, y)| x ^ y).collect();
println!("{:?}", c); }
[63, 10, 30, 0, 11]
239. Find first regular expression match
Assign to string x the first word of string s consisting of exactly 3 digits, or the empty string if no such match exists. A word containing more digits, or 3 digits as a substring fragment, must not match.
funcmain() { re := regexp.MustCompile(`\b\d\d\d\b`) for _, s := range []string{ "", "12", "123", "1234", "I have 12 goats, 3988 otters, 224 shrimps and 456 giraffes", "See p.456, for word boundaries", } { x := re.FindString(s) fmt.Printf("%q -> %q\n", s, x) } }
1 2 3 4 5 6
"" -> "" "12" -> "" "123" -> "123" "1234" -> "" "I have 12 goats, 3988 otters, 224 shrimps and 456 giraffes" -> "224" "See p.456, for word boundaries" -> "456"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
use regex::Regex;
fnmain() { letsentences = vec![ "", "12", "123", "1234", "I have 12 goats, 3988 otters, 224 shrimps and 456 giraffes", "See p.456, for word boundaries", ]; forsin sentences { letre = Regex::new(r"\b\d\d\d\b").expect("failed to compile regex"); letx = re.find(s).map(|x| x.as_str()).unwrap_or(""); println!("[{}] -> [{}]", &s, &x); } }