TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Reasoning with Types in Rust

162 pointsby miqktabout 7 years ago

5 comments

bringtheactionabout 7 years ago
&gt; A function of type fn&lt;T&gt;(T) -&gt; T must:<p>&gt; return its argument or<p>&gt; panic or abort or<p>&gt; never return<p>This confused me for a moment because I thought “why? it only says the return value must be the same type as the argument, not that it must have the same value”, but of course since the type can be anything there is no operation that is guaranteed to work that you can perform on the argument value. You can’t for example multiply by two because not all types will support multiplication by an integer.
评论 #16490267 未加载
评论 #16491627 未加载
评论 #16490625 未加载
评论 #16490470 未加载
bluejekyllabout 7 years ago
I loved this post. It’s very readable and introduces some interesting concepts related to type safety and some potentially simple use case in the context of access to private data, guaranteed from a type pattern.<p>As was pointed out in the Reddit conversation about this [1], the parametricity (new word I learned while reading this last night), will be lost with specialization.<p>Personally I want specialization for all the joy it will bring, and I don’t see this as a huge loss, but I wonder what other people’s thoughts are on this?<p>[1] <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;80eis2&#x2F;reasoning_with_types_in_rust&#x2F;?st=JE7ZUBNM&amp;sh=5df7aace" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;80eis2&#x2F;reasoning_with...</a>
评论 #16492683 未加载
skybrianabout 7 years ago
The <i>Secret</i> example is basically just encapsulation. It reminds me of the HTML type used in Go&#x27;s standard library to avoid XSS vulnerabilities in HTML templates due to lack of escaping. [1] Any string that&#x27;s not cast using HTML() is considered unsafe and will automatically be escaped.<p>In practice, this is used to make security audits easier. It&#x27;s not that you can&#x27;t defeat the system, but security review only needs to look at all the places a string is cast to or from the HTML type and verify that they&#x27;re in fact safe. This is considerably easier than it would be with bare strings, though better encapsulation would help. (In Go, this could be done with a struct that has a private field, but that&#x27;s not what they did for some reason.)<p>This is an old trick, also used by GWT [2] and other languages. Language support for zero-overhead, encapsulated wrapper types makes this particularly convenient; otherwise you have to decide if the performance cost is worth it for a widely-used type.<p>[1] <a href="https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;html&#x2F;template&#x2F;#HTML" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;html&#x2F;template&#x2F;#HTML</a> [2] <a href="http:&#x2F;&#x2F;www.gwtproject.org&#x2F;javadoc&#x2F;latest&#x2F;com&#x2F;google&#x2F;gwt&#x2F;safehtml&#x2F;shared&#x2F;SafeHtml.html" rel="nofollow">http:&#x2F;&#x2F;www.gwtproject.org&#x2F;javadoc&#x2F;latest&#x2F;com&#x2F;google&#x2F;gwt&#x2F;safe...</a>
评论 #16491968 未加载
评论 #16490233 未加载
drchickensaladabout 7 years ago
Site isn&#x27;t very usable on mobile.
评论 #16489965 未加载
评论 #16490822 未加载
评论 #16489170 未加载
评论 #16489126 未加载
rs86about 7 years ago
Excellent fucking article.