Skip to content

Commit 7a3a3f7

Browse files
committed
Merge pull request #14 from pocket7878/conditional-compilation
Conditional Compilation
2 parents 0efc482 + 6ca3dab commit 7a3a3f7

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed
Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
% Conditional Compilation
1+
% 条件付きコンパイル
2+
<!-- % Conditional Compilation -->
23

3-
Rust has a special attribute, `#[cfg]`, which allows you to compile code
4-
based on a flag passed to the compiler. It has two forms:
4+
<!-- Rust has a special attribute, `#[cfg]`, which allows you to compile code -->
5+
<!-- based on a flag passed to the compiler. It has two forms: -->
6+
Rustには `#[cfg]` という特別なアトリビュートがあり,
7+
コンパイラに渡されたフラグに合わせてコードをコンパイルすることを可能にします.
8+
`#[cfg]` アトリビュートは以下の2つの形式で利用することができます:
59

610
```rust
711
#[cfg(foo)]
@@ -11,7 +15,8 @@ based on a flag passed to the compiler. It has two forms:
1115
# fn bar() {}
1216
```
1317

14-
They also have some helpers:
18+
<!-- They also have some helpers: -->
19+
また,以下の様なヘルパーが存在します:
1520

1621
```rust
1722
#[cfg(any(unix, windows))]
@@ -24,70 +29,85 @@ They also have some helpers:
2429
# fn not_foo() {}
2530
```
2631

27-
These can nest arbitrarily:
32+
<!-- These can nest arbitrarily: -->
33+
ヘルパーは以下のように自由にネストすることが可能です:
2834

2935
```rust
3036
#[cfg(any(not(unix), all(target_os="macos", target_arch = "powerpc")))]
3137
# fn foo() {}
3238
```
3339

34-
As for how to enable or disable these switches, if you’re using Cargo,
35-
they get set in the [`[features]` section][features] of your `Cargo.toml`:
40+
<!-- As for how to enable or disable these switches, if you’re using Cargo, -->
41+
<!-- they get set in the [`[features]` section][features] of your `Cargo.toml`: -->
42+
このようなスイッチの有効・無効の切り替えはCargoを利用している場合「Cargo.toml」中の [`[features]` セクション][features] で設定できます.
3643

37-
[features]: http://doc.crates.io/manifest.html#the-features-section
44+
[フィーチャ]: http://doc.crates.io/manifest.html#the-features-section
3845

3946
```toml
4047
[features]
4148
# no features by default
4249
default = []
4350

44-
# The “secure-password” feature depends on the bcrypt package.
51+
# フィーチャ「secure-password」は bcrypt パッケージに依存しています
4552
secure-password = ["bcrypt"]
4653
```
4754

48-
When you do this, Cargo passes along a flag to `rustc`:
55+
<!-- When you do this, Cargo passes along a flag to `rustc`: -->
56+
もしこのように設定した場合,Cargoは `rustc` に以下のようにフラグを渡します:
4957

5058
```text
5159
--cfg feature="${feature_name}"
5260
```
5361

54-
The sum of these `cfg` flags will determine which ones get activated, and
55-
therefore, which code gets compiled. Let’s take this code:
62+
<!-- The sum of these `cfg` flags will determine which ones get activated, and -->
63+
<!-- therefore, which code gets compiled. Let’s take this code: -->
64+
渡されたすべての `cfg` フラグによってどのフラグが有効に成るか決定され,
65+
それによってどのコードがコンパイルされるかも決定されます.以下のコードを見てみましょう:
5666

5767
```rust
5868
#[cfg(feature = "foo")]
5969
mod foo {
6070
}
6171
```
6272

63-
If we compile it with `cargo build --features "foo"`, it will send the `--cfg
64-
feature="foo"` flag to `rustc`, and the output will have the `mod foo` in it.
65-
If we compile it with a regular `cargo build`, no extra flags get passed on,
66-
and so, no `foo` module will exist.
73+
<!-- If we compile it with `cargo build --features "foo"`, it will send the `--cfg -->
74+
<!-- feature="foo"` flag to `rustc`, and the output will have the `mod foo` in it. -->
75+
<!-- If we compile it with a regular `cargo build`, no extra flags get passed on, -->
76+
<!-- and so, no `foo` module will exist. -->
77+
もしこのコードを `cargo build --features "foo"` としてコンパイルを行うと,
78+
`--cfg features="foo"``rustc` に渡され,出力には `mod foo` が含まれます.
79+
もし標準的な `cargo build` でコンパイルを行った場合,`rustc` に追加のフラグは渡されず
80+
`foo` モジュールは存在しない事になります.
6781

6882
# cfg_attr
6983

70-
You can also set another attribute based on a `cfg` variable with `cfg_attr`:
84+
<!-- You can also set another attribute based on a `cfg` variable with `cfg_attr`: -->
85+
また,`cfg_attr` を用いることで,`cfg` に設定された値によってアトリビュートを有効にすることができます:
7186

7287
```rust
7388
#[cfg_attr(a, b)]
7489
# fn foo() {}
7590
```
7691

77-
Will be the same as `#[b]` if `a` is set by `cfg` attribute, and nothing otherwise.
92+
<!-- Will be the same as `#[b]` if `a` is set by `cfg` attribute, and nothing otherwise. -->
93+
このようにすると,`cfg` アトリビュートによって `a` が有効になっている場合に限り `#[b]` と設定されている
94+
場合と同じ効果が得られます.
7895

7996
# cfg!
8097

81-
The `cfg!` [syntax extension][compilerplugins] lets you use these kinds of flags
82-
elsewhere in your code, too:
98+
<!-- The `cfg!` [syntax extension][compilerplugins] lets you use these kinds of flags -->
99+
<!-- elsewhere in your code, too: -->
100+
`cfg!` [拡張構文][compilerplugins] は以下のようにコード中でフラグを利用することを可能にします:
83101

84102
```rust
85103
if cfg!(target_os = "macos") || cfg!(target_os = "ios") {
86104
println!("Think Different!");
87105
}
88106
```
89107

90-
[compilerplugins]: compiler-plugins.html
108+
[コンパイラプラグイン]: compiler-plugins.html
91109

92-
These will be replaced by a `true` or `false` at compile-time, depending on the
93-
configuration settings.
110+
<!-- These will be replaced by a `true` or `false` at compile-time, depending on the -->
111+
<!-- configuration settings. -->
112+
113+
このようなコードは設定に応じてコンパイル時に `true` または `false` に置き換えられます.

TranslationTable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
| crate | クレート
1919
| enum | 列挙型
2020
| `enum` | `enum`
21+
| feature | フィーチャ
2122
| Intrinsics | Intrinsic
2223
| Lang Items | Lang Item
2324
| lifetime | ライフタイム

0 commit comments

Comments
 (0)