What does the word "s" mean?

the codes show below:

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()
}

there is a word "s" in the "fn calculate_length" function sentence,but what does "s:&str" and "s.len()" mean?thx.

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.

Let’s reformat your code.

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.

The general pattern is:

fn my_function(first_argument: TypeOfFirstArg, second_argument: TypeOfSecondArg) -> ReturnType {
    // ...
}

where the number of arguments can be 0, 1, 2, 3, etc. (the pattern above is for two arguments).

For example a function that adds two integers (one of the integer types in Rust is i32):

fn add_two_integers(x: i32, y: i32) -> i32 {
    x + y
}

This function is the same as the following

fn add_two_integers(my_first_integer: i32, my_second_integer: i32) -> i32 {
    my_first_integer + my_second_integer
}

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

fn calculate_length(my_string_slice: &str) -> usize {
    my_string_slice.len()
}

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.

2 Likes

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();
}

thx.I'm all new in study programming language,so you know that... :sweat_smile: confused many times.

3 Likes

thank u,I could get the idea now.

1 Like

thx~

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.