Skip to content
This repository was archived by the owner on Jul 20, 2025. It is now read-only.

Commit 0dc490b

Browse files
committed
commit
1 parent 4999a79 commit 0dc490b

File tree

7 files changed

+102
-83
lines changed

7 files changed

+102
-83
lines changed

src/benchmarking/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ command for `cargo` which is executing all the methods annotated with the
55
`#[bench]` attribute. This attribute is currently [unstable][bench-unstable] and
66
available only for the nightly channel.
77

8-
.NET users can make use of `BenchmarkDotNet` library to benchmark methods and
9-
track their performance. The equivalent of `BenchmarkDotNet` is a crate named
8+
.NET users can make use of `Benchmark.js` library to benchmark methods and
9+
track their performance. The equivalent of `Benchmark.js` is a crate named
1010
`Criterion`.
1111

1212
As per its [documentation][criterion-docs], `Criterion` collects and stores
@@ -16,8 +16,8 @@ regressions as well as measuring optimizations.
1616
Using `Criterion` is possible to use the `#[bench]` attribute without moving to
1717
the nightly channel.
1818

19-
As in `BenchmarkDotNet`, it is also possible to integrate benchmark results with
20-
the [GitHub Action for Continuous Benchmarking][gh-action-bench]. `Criterion`,
19+
<!--As in `BenchmarkDotNet`, it is also possible to integrate benchmark results with
20+
the [GitHub Action for Continuous Benchmarking][gh-action-bench]. -->`Criterion`,
2121
in fact, supports multiple output formats, amongst which there is also the
2222
`bencher` format, mimicking the nightly `libtest` benchmarks and compatible with
2323
the above mentioned action.

src/conditional-compilation/index.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Conditional Compilation
22

3-
Both .NET and Rust are providing the possibility for compiling specific code
3+
Both JavaScript and Rust are providing the possibility for compiling specific code
44
based on external conditions.
55

6-
In .NET it is possible to use the some [preprocessor directives][preproc-dir] in
7-
order to control conditional compilation
6+
JavaScript doesn't support conditional compilation natively. However, it is possible to use some third-party tool like [`babel-plugin-preprocessor`][preproc-dir] in
7+
order to control conditional compilation.
88

9-
```csharp
10-
#if debug
9+
```js
10+
//#if DEBUG
1111
Console.WriteLine("Debug");
12-
#else
12+
//#else
1313
Console.WriteLine("Not debug");
14-
#endif
14+
//#endif
1515
```
1616

1717
In addition to predefined symbols, it is also possible to use the compiler
@@ -22,9 +22,6 @@ In Rust it is possible to use the [`cfg attribute`][cfg],
2222
the [`cfg_attr attribute`][cfg-attr] or the
2323
[`cfg macro`][cfg-macro] to control conditional compilation
2424

25-
As per .NET, in addition to predefined symbols, it is also possible to use the
26-
[compiler flag `--cfg`][cfg-flag] to arbitrarily set configuration options
27-
2825
The [`cfg attribute`][cfg] is requiring and evaluating a
2926
`ConfigurationPredicate`
3027

@@ -110,5 +107,5 @@ See also:
110107
[cfg-flag]: https://doc.rust-lang.org/rustc/command-line-arguments.html#--cfg-configure-the-compilation-environment
111108
[cfg-attr]: https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute
112109
[cfg-macro]: https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-macro
113-
[preproc-dir]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#conditional-compilation
114-
[DefineConstants]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/language#defineconstants
110+
[preproc-dir]: https://github.com/kaysonwu/babel-plugin-preprocessor
111+
[DefineConstants]: https://github.com/kaysonwu/babel-plugin-preprocessor?tab=readme-ov-file#usage

src/environment-and-configuration/index.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,32 @@
22

33
## Accessing environment variables
44

5-
.NET provides access to environment variables via the
6-
`System.Environment.GetEnvironmentVariable` method. This method retrieves the
7-
value of an environment variable at runtime.
5+
JavaScript doesn't provide access to environment variables natively. However, some non-browser JavaScript runtimes, such as Node.js and Node provides access to environment variables.
6+
7+
In Node.js:
8+
9+
```js
10+
const name = "EXAMPLE_VARIABLE";
11+
12+
let value = process.env[name];
13+
if (!value) {
14+
console.log(`Variable '${name}' not set.`);
15+
} else {
16+
console.log(`Variable '${name}' set to '${value}'.`);
17+
}
18+
```
19+
In Deno:
20+
21+
```js
22+
const name = "EXAMPLE_VARIABLE";
23+
24+
let value = Deno.env.get(name);
25+
if (!value) {
26+
console.log(`Variable '${name}' not set.`);
27+
} else {
28+
console.log(`Variable '${name}' set to '${value}'.`);
29+
}
30+
```
831

932
```csharp
1033
using System;
@@ -68,14 +91,10 @@ fn main() {
6891
}
6992
```
7093

71-
In .NET a compile time access to environment variables can be achieved, in a
72-
less straightforward way, via [source generators][source-gen].
73-
74-
[source-gen]: https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview
75-
7694
## Configuration
7795

78-
Configuration in .NET is possible with configuration providers. The framework is
96+
JavaScript doesn't support configurations.
97+
<!--Configuration in .NET is possible with configuration providers. The framework is
7998
providing several provider implementations via
8099
`Microsoft.Extensions.Configuration` namespace and NuGet packages.
81100
@@ -105,7 +124,9 @@ Other providers examples can be found in the official documentation
105124
106125
A similar configuration experience in Rust is available via use of third-party
107126
crates such as [figment] or [config].
108-
127+
-->
128+
In Rust it is available via use of third-party
129+
crates such as [figment] or [config].
109130
See the following example making use of [config] crate:
110131

111132
```rust

src/language/scalar-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ JavaScript:
55

66
| Rust | JavaScript | Note |
77
| ------- | ------------------------- | ----------- |
8-
| `bool` | `boolean` | |
8+
| `bool` | `boolean` | |
99
| `char` | `string` | See note 1. |
1010
| `i8` | `number` | See note 2. |
1111
| `i16` | `number` | |

src/logging-and-tracing/index.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
# Logging and Tracing
22

3-
.NET supports a number of logging APIs. For most cases, `ILogger` is a good
4-
default choice, since it works with a variety of built-in and third-party
5-
logging providers. In C#, a minimal example for structured logging could look
3+
For most cases, `console.log()` is a good
4+
default choice for JavaScript, since it works with a variety of built-in and third-party
5+
logging providers. In JavaScript, a minimal example for structured logging could look
66
like:
77

8-
```csharp
9-
using Microsoft.Extensions.Logging;
10-
11-
using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
12-
var logger = loggerFactory.CreateLogger<Program>();
13-
logger.LogInformation("Hello {Day}.", "Thursday"); // Hello Thursday.
8+
```js
9+
let day = "Thursday";
10+
console.log("Hello ", day); // Hello Thursday.
1411
```
1512

1613
In Rust, a lightweight logging facade is provided by [log][log.rs]. It has less

src/testing/index.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## Test organization
44

5-
.NET solutions use separate projects to host test code, irrespective of the
6-
test framework being used (xUnit, NUnit, MSTest, etc.) and the type of tests
7-
(unit or integration) being wirtten. The test code therefore lives in a
5+
JavaScript solutions use separate projects to host test code, irrespective of the
6+
test framework being used (Jest, Mocha, etc.) and the type of tests
7+
(unit or integration) being written. The test code therefore lives in a
88
separate assembly than the application or library code being tested. In Rust,
99
it is a lot more conventional for _unit tests_ to be found in a separate test
1010
sub-module (conventionally) named `tests`, but which is placed in same _source
@@ -14,7 +14,7 @@ tests. This has two benefits:
1414
- The code/module and its unit tests live side-by-side.
1515

1616
- There is no need for a workaround like `[InternalsVisibleTo]` that exists in
17-
.NET because the tests have access to internals by virtual of being a
17+
JavaScript because the tests have access to internals by virtual of being a
1818
sub-module.
1919

2020
The test sub-module is annotated with the `#[cfg(test)]` attribute, which has
@@ -67,17 +67,23 @@ For more information, see "[Showing Function Output][test-output]".
6767
[test-output]: https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output
6868

6969
## Assertions
70+
JavaScript users have multiple ways to assert, depending on the framework being used. For example, an assertion Jest might look like:
71+
```js
72+
test('something has the right length', () => {
73+
let value = "something";
74+
expect(value.length).toBe(9);
75+
});
76+
```
7077

71-
.NET users have multiple ways to assert, depending on the framework being
72-
used. For example, an assertion xUnit.net might look like:
78+
An example that only uses vanilla JavaScript:
7379

74-
```csharp
75-
[Fact]
76-
public void Something_Is_The_Right_Length()
77-
{
78-
var value = "something";
79-
Assert.Equal(9, value.Length);
80+
```js
81+
function somethingIsTheRightLength() {
82+
let value = "something";
83+
console.assert(value.length === 9);
8084
}
85+
86+
somethingIsTheRightLength();
8187
```
8288

8389
Rust does not require a separate framework or crate. The standard library
@@ -107,9 +113,9 @@ tests, such as `[Theory]` in xUnit.net.
107113

108114
## Mocking
109115

110-
When writing tests for a .NET application or library, there exist several
111-
frameworks, like Moq and NSubstitute, to mock out the dependencies of types.
112-
There are similar crates for Rust too, like [`mockall`][mockall], that can
116+
<!--When writing tests for a .NET application or library, there exist several
117+
frameworks, like Moq and NSubstitute, to mock out the dependencies of types.-->
118+
There are<!-- similar--> crates for Rust too, like [`mockall`][mockall], that can
113119
help with mocking. However, it is also possible to use [conditional
114120
compilation] by making use of the [`cfg` attribute][cfg-attribute] as a simple
115121
means to mocking without needing to rely on external crates or frameworks. The
@@ -181,10 +187,8 @@ mod tests {
181187

182188
## Code coverage
183189

184-
There is sophisticated tooling for .NET when it comes to analyzing test code
185-
coverage. In Visual Studio, the tooling is built-in and integrated. In Visual
186-
Studio Code, plug-ins exist. .NET developers might be familiar with [coverlet]
187-
as well.
190+
There is sophisticated tooling for JavaScript when it comes to analyzing test code
191+
coverage, such as Jest.
188192

189193
Rust is providing [built-in code coverage implementations][built-in-cov] for
190194
collecting test code coverage.
@@ -217,6 +221,5 @@ in-line visual indicators about the line coverage in the source code editor.
217221
218222
[coverage.gutters]: https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters
219223
[tarpaulin]: https://github.com/xd009642/tarpaulin
220-
[coverlet]: https://github.com/coverlet-coverage/coverlet
221224
[built-in-cov]: https://doc.rust-lang.org/stable/rustc/instrument-coverage.html#test-coverage
222225
[project structure]: ../project-structure/index.md

src/threading/synchronization.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
# Synchronization
22

33
When data is shared between threads, one needs to synchronize read-write
4-
access to the data in order to avoid corruption. The C# offers the `lock`
5-
keyword as a synchronization primitive (which desugars to exception-safe use
6-
of `Monitor` from .NET):
7-
8-
```csharp
9-
using System;
10-
using System.Threading;
11-
12-
var dataLock = new object();
13-
var data = 0;
14-
var threads = new List<Thread>();
15-
16-
for (var i = 0; i < 10; i++)
17-
{
18-
var thread = new Thread(() =>
19-
{
20-
for (var j = 0; j < 1000; j++)
21-
{
22-
lock (dataLock)
23-
data++;
4+
access to the data in order to avoid corruption. In JavaScript:
5+
6+
```js
7+
let data = 0;
8+
let workers = [];
9+
let completedWorkers = 0;
10+
11+
for (let i = 0; i < 10; i++) {
12+
let worker = new Worker('data:text/javascript,' + encodeURIComponent(`
13+
let partialData = 0;
14+
for (let j = 0; j < 1000; j++) {
15+
partialData++;
2416
}
25-
});
26-
threads.Add(thread);
27-
thread.Start();
17+
self.postMessage(partialData);
18+
`));
19+
20+
worker.onmessage = function(event) {
21+
data += event.data;
22+
completedWorkers++;
23+
if (completedWorkers === 10) {
24+
console.log(data);
25+
workers.forEach(function(worker) {
26+
worker.terminate();
27+
});
28+
}
29+
};
30+
31+
workers.push(worker);
32+
worker.postMessage(null);
2833
}
2934

30-
foreach (var thread in threads)
31-
thread.Join();
32-
33-
Console.WriteLine(data);
3435
```
3536

3637
In Rust, one must make explicit use of concurrency structures like `Mutex`:

0 commit comments

Comments
 (0)