// Created on savesnippets.com · https://savesnippets.com/8ROamtLO4cVL8b fn sum(values: &[i32]) -> i32 { // accepts any slice values.iter().sum() } fn first_word(s: &str) -> &str { // returns a borrowed slice match s.find(' ') { Some(i) => &s[..i], None => s, } } fn main() { let v = vec![1, 2, 3, 4, 5]; println!("{}", sum(&v)); // pass whole Vec as slice println!("{}", sum(&v[1..4])); // pass sub-slice — no copy let s = String::from("hello world"); println!("first: {}", first_word(&s)); // "hello" // s is still owned and usable }