This question already has answers here:
How do I see the expanded macro code that's causing my compile error?
(4 answers)
Closed 4 years ago.
How can I see the code output for expanded Rust macros?
For example I have this snippet:
macro_rules! five_times {
($x:expr) => (5 * $x);
}
fn main() {
let a = five_times!(2 + 3);
}
And I want to see something like this:
fn main() {
let a = 5 * (2 + 3);
}
When using nightly Rust you can use the following command on a source file:
rustc --pretty expanded -Z unstable-options FILENAME.rs
This will print the following output:
macro_rules! five_times(( $ x : expr ) => ( 5 * $ x ) ;);
fn main() { let a = 5 * (2 + 3); }
Update
With the 2021 version the command changed to (thanks #at54321):
rustc -Zunpretty=expanded FILENAME.rs
Related
Want achieve in swift (MacOS) the following dead-simple perl script:
use strict;
use warnings;
for my $arg (#ARGV) { #loop over arguments
next if $arg !~ /^\d+$/; #skip if not all digits
print("arg:", $arg + 2, "\n"); #print the number + 2...
}
so invoking this script as: perl script.pl 10 20 30 prints
arg:12
arg:22
arg:32
My "experiment" in swift script called arg.swift:
import Darwin // otherwise the "exit" won't works...
if CommandLine.argc < 2 {
print("Error", CommandLine.arguments[0], " No arguments are passed.")
exit(1)
}
for i in 1 ..< CommandLine.argc { // in the "0" is the program name (as above) so skip it
print("arg:", CommandLine.arguments[i] + 2) // add 2 and print the result
}
running it as swift arg.swift 10 20 30 , prints the following errors..
arg.swift:9:44: error: cannot subscript a value of type '[String]' with an index of type 'Int32'
print("arg:", CommandLine.arguments[i] + 2) // add 2 and print the result
^
arg.swift:9:44: note: overloads for 'subscript' exist with these partially matching parameter lists: (Int), (Range<Int>), (Range<Self.Index>), ((UnboundedRange_) -> ())
print("arg:", CommandLine.arguments[i] + 2) // add 2 and print the result
Honestly, absolutely don't understand what is wrong, so why CommandLine.arguments[0] works, and it complains about CommandLine.arguments[i]... Also, what(?) about overloads?
if someone needs, using:
$ swift --version
Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)
Target: x86_64-apple-darwin17.7.0
All arguments are passed as Strings, so if you want to use them as Ints, you need to add the conversion.
Also, argc is an UInt32 and needs to be converted as well so that you can use it as a subscript index.
For example, like so:
for i in 1 ..< Int(CommandLine.argc) {
if let intValue = Int(CommandLine.arguments[i]) {
print("arg:", intValue + 2)
}
}
This question already has answers here:
How to use trailing closure in if condition?
(2 answers)
Using trailing closure in for-in loop
(3 answers)
Swift: Can not use array filter in if let statement condition
(1 answer)
Closed 5 years ago.
Noob here.
Why is this ok:
let lockedCount = myStructArray.filter{$0.isLocked == true}.count
and this not ok:
if myStructArray.filter{$0.isLocked == true}.count < 4 {
print("Fewer than 4 locked")
}
Swift has trouble parsing anonymous closure in the context of an if logical expression. You can work around this issue by parenthesizing the count expression:
if (myStructArray.filter{$0.isLocked == true}.count) < 4 {
// ^ ^
print("Fewer than 4 locked")
}
or
if (myStructArray.filter{$0.isLocked == true}.count < 4) {
// ^ ^
print("Fewer than 4 locked")
}
or
if myStructArray.filter({$0.isLocked == true}).count < 4 {
// ^ ^
print("Fewer than 4 locked")
}
This question already has answers here:
How to iterate for loop in reverse order in swift?
(16 answers)
Reverse Range in Swift
(7 answers)
Closed 4 years ago.
for var i=count-2; i>=0; --i
{
if let nextControlPoint = firstControlPoints[i+1]
{
let controlPointX = (rhsArray[i].x.f - c[i] * nextControlPoint.x.f)/b[i]
let controlPointY = (rhsArray[i].y.f - c[i] * nextControlPoint.y.f)/b[i]
}
z += 1
}
You can write something like this
if count > 1 {
for i in (0...count-2).reversed() {
print(i)
}
}
The IF statement is needed because we cannot create a range where the first element is lower than the last one.
This question already has answers here:
#warning: C-style for statement is deprecated and will be removed in a future version of Swift [duplicate]
(4 answers)
Decrement index in a loop after Swift C-style loops deprecated
(5 answers)
Closed 6 years ago.
I facing problem in executing for loop in Swift 3. I can use loop for range operator ... and ..< but in may case, I want something like ..> but its not available.
How do I execute following loop in Swift 3?
var myMax = 20
for var i = myMax ; i >= 0 ; i -= 1 {
...
}
It's easy to reverse the loop. User reversed function.
Swift 3
let myMax = 20
for i in (1..<myMax).reversed() {
print(i)
}
You can also use stride as #ZaidPathan said :
This question have all answers with all versions : How to iterate for loop in reverse order in swift?
for i in (1..<20).reversed() {
print(i)
}
Hope it helps.Read More
Swift 3, You can use stride
var myMax = 20
for var i = myMax ; i > 0 ; i -= 1 {
}
//Equivalent
let myMax = 20
for value in stride(from: 20, to: 0, by: -1){
print(value)
}
var myMax = 20
for var i = myMax ; i >= 0 ; i -= 1 {
}
//Equivalent
for value in stride(from: 20, through: 0, by: -1){
print(value)
}
I've got this question, and I'm a bit confused as to what would be printed, especially for pass-by-reference. What value would be passed to x if there are two parameters? Thanks!
Consider the following program. For each of the following parameter-passing methods, what is printed?
a. Passed by value
b. Passed by reference
c. Passed by value-result
void main()
{
int x = 5;
foo (x,x);
print (x);
}
void foo (int a, int b)
{
a = 2 * b + 1;
b = a - 1;
a = 3 * a - b;
}
The first two should be pretty straightforward, the last one is probably throwing you because it's not really a C++ supported construct. It's something that had been seen in Fortran and Ada some time ago. See this post for more info
As for your results, I think this is what you would get:
1)
5
2)
x = 5,
a = 2 * 5 + 1 = 11
b = 11 - 1 = 10
a = 3 * 10 - 10 = 20; // remember, a and b are the same reference!
x = 20
3) Consider this (in C++ style). We will copy x into a variable, pass that by reference, and then copy the result back to x:
void main()
{
int x = 5;
int copy = x;
foo (copy,copy); // copy is passed by reference here, for sake of argument
x = copy;
print (x);
}
Since nothing in the foo function is doing anything with x directly, your result will be the same as in #2.
Now, if we had something like this for foo
void foo (int a, int b)
{
a = 2 * b + 1;
x = a - 1; // we'll assume x is globally accessible
a = 3 * a - b;
}
Then # 2 would produce the same result, but #3 would come out like so:
a = 2 * 5 + 1 = 11
x = 11 - 1 = 10 // this no longer has any effect on the final result
a = 3 * 11 - 11 = 22
x = 22