Похожие чаты

Hi, why is a String literal mutable by default when

declared. But not mutable by default when passed as argument to some function?

5 ответов

76 просмотров

String literals are immutable in many programming languages. When you declare a string literal, it's not mutable by default; it's actually immutable. When you pass a string to a function, the immutability remains. If it seems mutable in some contexts, it's because many languages allow syntactic sugar to make strings appear mutable, or they provide mutable versions of string-like data structures. However, when passed as an argument, the function receives a reference or copy of the string, not the original string itself, preserving immutability.

Бетонная-Тень Автор вопроса
Аналитический Жонглёр
String literals are immutable in many programming ...

For example, in main function s is not mutable, but I can pass it to the make_awesome function which needs mut String. fn main() { let s = String::from("Rust"); let s = make_awesome(s); println!("{s}") } fn make_awesome(mut s: String) -> String { s.push_str(" is awesome!"); return s }

Бетонная Тень
For example, in main function s is not mutable, bu...

well, here you _move_ your string completely (i.e. give up on it completely), and make_awesome can do anything with it, including mutating it

Бетонная-Тень Автор вопроса
Бежевый Вальс
well, here you _move_ your string completely (i.e....

Then isn't it redundant to put mut in function argument (in this case)? Since it is anyway taking ownership?

Бетонная Тень
Then isn't it redundant to put mut in function arg...

if you don't mark it as mutable, you won't be able to mutate it

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Мелодичный Сырник
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Фетровый Лингвист
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Настойчивый Кварк
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
Льдистая Лужица
2
30500 за редактор? )
Замшевый Алхимик
47
а через ESC-код ?
Виноградная Притча
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Звенящий Циферблат
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Туманный Ларец
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Длинный Минерал
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Тягучая Ракушка
11
Карта сайта