s is just short for string, a variable name. : is for declaring the type and &str is the declared type. s.len() desugars to str::len(&s) which calls the associated function len on the str type with &s passed in as an argument. len is short for length.
fn main() {
let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len);
}
fn calculate_length(s: &str) -> usize {
s.len()
}
Try out the “Rustfmt” button under “TOOLS” in the rust playground to format your rust code nicely and read the pinned post on how to post it in this forum properly.
where x and y are renamed. In the parameter list, the names are declared, which means you can choose whatever names you like for your arguments. In the function body (inside { }) you then have to use those names you chose. Your calculate_length function could thus equivalently be written as
The s has no special meaning, it is just an identifier. The s: &str means “s is an argument of type &str” and s.len() means “call the method called len on s”.
In my version of calculate_length, correspondingly, the my_string_slice: &str means “my_string_slice is an argument of type &str” and my_string_slice.len() means “call the method called len on my_string_slice”.
Finally, the fact that the s.len() expression is the final (and only) expression in the calculate_length function (and does not have any ; behind it) means that the result of the method call to len is the value that’s returned from the calculate_length function.
Similarly the x + y in the add_two_integers example above is also returned, which means that the sum of x and y will be the return value of the add_two_integers function.
Also just fyi, your question isn't really a rust specific question either. The same kind of logic applies to pretty much any programming language. There might be minor variations but the general idea is the same.
For example in java it would be something like
public int calculateLength(String s) {
return s.length();
}