🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #47

Re: Optimization by programmers

#47
From: David Brown
Date: Mon, 12 Oct 2026 05:47
66 lines
3925 bytes
On 11/10/2026 20:56, Terje Mathisen wrote:
> MitchAlsup wrote:
>>
>> Terje Mathisen <terje.mathisen@tmsw.no> posted:
>>
>>> Dan Cross wrote:
>> ---------
>>>
>>> My old asm/C brain _knows_ that a slice of something with a known
>>> address and total length is effectively the same thing as an unmutable
>>> array.
>>
>> Can I get a definition of what mutable in Rust means ??
> 
> It does mean that the function which receives a non-mut parameter, or 
> itself declares som non-mut local variables, cannot modify any of them.
> 
> So yes, very similar to "not const".

It sounds more powerful than "not const" in C.

This is perfectly reasonable C code :

	void change(const int * p, int x) {
		int * q = (int *) p;
		*q = x;
	}

Calling "change" with a pointer to data that was defined with a 
const-qualified type is undefined behaviour.  Other than that, the 
behaviour is fully defined.

So you may not change the value of a variable /defined/ as "const" in C 
- any attempt to do so is UB, and unless you go out of your way (such as 
with pointer casts), accidental attempts will usually be flagged as 
compile-time errors.

But if you receive a "pointer to const" parameter to a function, you can 
still write via a cast version of the pointer if the original object was 
not defined as "const".  (So objects defined as "const" may be placed in 
read-only memory of some kind, and trying to change them might cause a 
crash, signal, bus-error, etc.)

(In C++, members of a class can be marked "mutable" so that they can be 
changed without even needing any pointer cast shenanigans.  This means 
you can have classes where instances are "logically constant" in a 
function call - their user-visible value does not change - but with 
aspects that do change, such as caching the results of a calculation, or 
re-ordering elements in the underlying data structure for a set.)

The weaker promise of "const" in C and C++ blocks some possible compiler 
optimisations of code, but allows some additional user flexibility.

I think (and you will hopefully correct me if I am wrong!) in Rust if a 
function receives a non-mutable reference as a parameter, it is not 
allowed to cast it to a mutable reference.

> 
> You can discuss if its a good idea to make non-mutability the default 
> instead of the opposite. Personally I like that part.
> 

It certainly sounds good to me.


Technical Details
Message-ID: <11ahs8q$38ir0$4@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <1177fdi$26ea4$1@dont-email.me> <11a3ofn$2ak3s$1@dont-email.me> <11a5hd5$4qb$1@reader2.panix.com> <11a5lqo$2v5bq$1@dont-email.me> <11a6870$41j$1@reader2.panix.com> <11a6a6h$37i1n$1@dont-email.me> <1791742457-5857@newsgrouper.org> <11agm4p$2tfut$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.arch
Subject: Re: Optimization by programmers
Date: Mon, 12 Oct 2026 07:47:06 +0200
Organization: A noiseless patient Spider
Lines: 66
Message-ID: <11ahs8q$38ir0$4@dont-email.me>
References: <1177fdi$26ea4$1@dont-email.me> <11a3ofn$2ak3s$1@dont-email.me>
 <11a5hd5$4qb$1@reader2.panix.com> <11a5lqo$2v5bq$1@dont-email.me>
 <11a6870$41j$1@reader2.panix.com> <11a6a6h$37i1n$1@dont-email.me>
 <1791742457-5857@newsgrouper.org> <11agm4p$2tfut$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 12 Oct 2026 05:47:06 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3427168"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+uPzPzfx9JrwkW3rPLXzZXbYJ6gt1ulQQ="; posting-host="711f10de653bed0662d2460037681553"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gl+DiYpEWKuKjD7xvCcwEytEUiY= sha256:EoYeK7DaWdjNSQYXC8Bzonk6JxyDLkQ8fO1XT3efnsE=
	sha1:b75Kd/APMJK9ffxMzKwC+9RSN7Q= sha256:xI51qnKF32zbksMTnQk2ztk5ItuJINdMxeko526o+EE=
In-Reply-To: <11agm4p$2tfut$1@dont-email.me>
Content-Language: en-GB, nb-NO