1
- % Conditional Compilation
1
+ % 条件付きコンパイル
2
+ <!-- % Conditional Compilation -->
2
3
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つの形式で利用することができます:
5
9
6
10
``` rust
7
11
#[cfg(foo)]
@@ -11,7 +15,8 @@ based on a flag passed to the compiler. It has two forms:
11
15
# fn bar () {}
12
16
```
13
17
14
- They also have some helpers:
18
+ <!-- They also have some helpers: -->
19
+ また,以下の様なヘルパーが存在します:
15
20
16
21
``` rust
17
22
#[cfg(any(unix, windows))]
@@ -24,70 +29,85 @@ They also have some helpers:
24
29
# fn not_foo () {}
25
30
```
26
31
27
- These can nest arbitrarily:
32
+ <!-- These can nest arbitrarily: -->
33
+ ヘルパーは以下のように自由にネストすることが可能です:
28
34
29
35
``` rust
30
36
#[cfg(any(not(unix), all(target_os= " macos" , target_arch = " powerpc" )))]
31
37
# fn foo () {}
32
38
```
33
39
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 ] で設定できます.
36
43
37
- [ features ] : http://doc.crates.io/manifest.html#the-features-section
44
+ [ フィーチャ ] : http://doc.crates.io/manifest.html#the-features-section
38
45
39
46
``` toml
40
47
[features ]
41
48
# no features by default
42
49
default = []
43
50
44
- # The “ secure-password” feature depends on the bcrypt package.
51
+ # フィーチャ「 secure-password」は bcrypt パッケージに依存しています
45
52
secure-password = [" bcrypt" ]
46
53
```
47
54
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 ` に以下のようにフラグを渡します:
49
57
50
58
``` text
51
59
--cfg feature="${feature_name}"
52
60
```
53
61
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
+ それによってどのコードがコンパイルされるかも決定されます.以下のコードを見てみましょう:
56
66
57
67
``` rust
58
68
#[cfg(feature = " foo" )]
59
69
mod foo {
60
70
}
61
71
```
62
72
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 ` モジュールは存在しない事になります.
67
81
68
82
# cfg_attr
69
83
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 ` に設定された値によってアトリビュートを有効にすることができます:
71
86
72
87
``` rust
73
88
#[cfg_attr(a, b)]
74
89
# fn foo () {}
75
90
```
76
91
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
+ 場合と同じ効果が得られます.
78
95
79
96
# cfg!
80
97
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 ] は以下のようにコード中でフラグを利用することを可能にします:
83
101
84
102
``` rust
85
103
if cfg! (target_os = " macos" ) || cfg! (target_os = " ios" ) {
86
104
println! (" Think Different!" );
87
105
}
88
106
```
89
107
90
- [ compilerplugins ] : compiler-plugins.html
108
+ [ コンパイラプラグイン ] : compiler-plugins.html
91
109
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 ` に置き換えられます.
0 commit comments