Похожие чаты

Hi, why is a String literal mutable by default when

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

5 ответов

38 просмотров

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.

Sudeep- Автор вопроса
Vladisλav
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 }

Sudeep
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

Sudeep- Автор вопроса
Artem Starikov
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?

Sudeep
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

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

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

30500 за редактор? )
Владимир
47
any reference of this implementation?
BitBuddha
29
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Недавно Google Project Zero нашёл багу в SQLite с помощью LLM, о чём достаточно было шумно в определённых интернетах, которые сопровождались рассказами, что скоро всех "ибешни...
Alex Sherbakov
5
So much speculation in the last week. So much volatility in price. This is because Hedera has a GC that isn't using the network it's governing. Why aren't people asking why a...
Summit Seeker R
8
Anyone else having this error when trying to make transactions?
Datzel
11
Question: How viable is it to use Anvil as the backend infrastructure for managing a TradFi portfolio, while integrating Flexa for instant liquidity and payment solutions? Cou...
Kevin
2
вы делали что-то подобное и как? может есть либы готовые? увидел картинку нокода, где всё линиями соединено и стало интересно попробовать то же в ddl на lua сделать. решил с ч...
Victor
8
Карта сайта