diff options
-rw-r--r-- | chapel.html.markdown | 822 | ||||
-rw-r--r-- | elixir.html.markdown | 2 | ||||
-rw-r--r-- | es-es/git-es.html.markdown | 2 | ||||
-rw-r--r-- | haskell.html.markdown | 9 | ||||
-rw-r--r-- | matlab.html.markdown | 2 | ||||
-rw-r--r-- | perl6.html.markdown | 11 | ||||
-rw-r--r-- | python.html.markdown | 11 | ||||
-rw-r--r-- | python3.html.markdown | 16 | ||||
-rw-r--r-- | rust.html.markdown | 2 | ||||
-rw-r--r-- | zh-cn/groovy-cn.html.markdown | 420 | ||||
-rw-r--r-- | zh-cn/rust-cn.html.markdown | 296 |
11 files changed, 1568 insertions, 25 deletions
diff --git a/chapel.html.markdown b/chapel.html.markdown new file mode 100644 index 00000000..53e9747c --- /dev/null +++ b/chapel.html.markdown @@ -0,0 +1,822 @@ +--- +language: chapel +filename: learnchapel.chpl +contributors: + - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] +--- + +You can read all about Chapel at [Cray's official Chapel website](http://chapel.cray.com). +In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. + +More information and support can be found at the bottom of this document. + +```c +// Comments are C-family style +// one line comment +/* + multi-line comment +*/ + +// Basic printing +write( "Hello, " ); +writeln( "World!" ); +// write and writeln can take a list of things to print. +// each thing is printed right next to each other, so include your spacing! +writeln( "There are ", 3, " commas (\",\") in this line of code" ); +// Different output channels +stdout.writeln( "This goes to standard output (just like plain writeln( ) does)"); +stderr.writeln( "This goes to standard error" ); + +// Variables don't have to be explicitly typed as long as +// the compiler can figure out the type that it will hold. +var myVar = 10; // 10 is an int, so myVar is implicitly an int +myVar = -10; +var mySecondVar = myVar; +// var anError; // this would be a compile time error. + +// We can (and should) explicitly type things +var myThirdVar: real; // define mySecondVar as a real +var myFourthVar: real = -1.234; +myThirdVar = myFourthVar; + +// There are a number of basic types. +var myInt: int = -1000; // Signed ints +var myUint: uint = 1234; // Unsigned ints +var myReal: real = 9.876; // Floating point numbers +var myImag: imag = 5.0i; // Imaginary numbers +var myCplx: complex = 10 + 9i; // Complex numbers +myCplx = myInt + myImag ; // Another way to form complex numbers +var myBool: bool = false; // Booleans +var myStr: string = "Some string..."; // Strings + +// Some types can have sizes +var my8Int: int(8) = 10; // 8 bit (one byte) sized int; +var my64Real: real(64) = 1.516; // 64 bit (8 bytes) sized real + +// Typecasting +var intFromReal = myReal : int; +var intFromReal2: int = myReal : int; + +// consts are constants, they cannot be changed after set in runtime +const almostPi: real = 22.0/7.0; + +// params are constants whose value must be known statically at compile time +// Like consts, they cannot be changed during runtime +param compileTimeConst: int = 16; + +// The config modifier allows values to be set at the command line +// and is much easier that the usual getOpts debacle +// config vars and consts can be changed through the command line at run time +config var varCmdLineArg: int = -123; +config const constCmdLineArg: int = 777; +// Set with --VarName=Value or --VarName Value at run time + +// config params can be set at compile time +config param paramCmdLineArg: bool = false; +writeln( varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg ); +// Set config with --set paramCmdLineArg=value at compile time + +// refs operate much like a reference in C++ +var actual = 10; +ref refToActual = actual; // refToActual refers to actual +writeln( actual, " == ", refToActual ); // prints the same value +actual = -123; // modify actual (which refToActual refers to) +writeln( actual, " == ", refToActual ); // prints the same value +refToActual = 99999999; // modify what refToActual refers to (which is actual) +writeln( actual, " == ", refToActual ); // prints the same value + +// Math operators +var a: int, thisInt = 1234, thatInt = 5678; +a = thisInt + thatInt; // Addition +a = thisInt * thatInt; // Multiplication +a = thisInt - thatInt; // Subtraction +a = thisInt / thatInt; // Division +a = thisInt ** thatInt; // Exponentiation +a = thisInt % thatInt; // Remainder (modulo) + +// Logical Operators +var b: bool, thisBool = false, thatBool = true; +b = thisBool && thatBool; // Logical and +b = thisBool || thatBool; // Logical or +b = !thisBool; // Logical negation + +// Relational Operators +b = thisInt > thatInt; // Greater-than +b = thisInt >= thatInt; // Greater-than-or-equal-to +b = thisInt < a && a <= thatInt; // Less-than, and, less-than-or-equal-to +b = thisInt != thatInt; // Not-equal-to +b = thisInt == thatInt; // Equal-to + +// Bitwise operations +a = thisInt << 10; // Left-bit-shift by 10 bits; +a = thatInt >> 5; // Right-bit-shift by 5 bits; +a = ~thisInt; // Bitwise-negation +a = thisInt ^ thatInt; // Bitwise exclusive-or + +// Compound assignment operations +a += thisInt; // Addition-equals ( a = a + thisInt;) +a *= thatInt; // Times-equals ( a = a * thatInt; ) +b &&= thatBool; // Logical-and-equals ( b = b && thatBool; ) +a <<= 3; // Left-bit-shift-equals ( a = a << 10; ) +// and many, many more. +// Unlike other C family languages there are no +// pre/post-increment/decrement operators like +// ++j, --j, j++, j-- + +// Swap operator +var old_this = thisInt; +var old_that = thatInt; +thisInt <=> thatInt; // Swap the values of thisInt and thatInt +writeln( (old_this == thatInt) && (old_that == thisInt) ); + +// Operator overloads can also be defined, as we'll see with procedures + +// Tuples can be of the same type +var sameTup: 2*int = (10,-1); +var sameTup2 = (11, -6); +// or different types +var diffTup: (int,real,complex) = (5, 1.928, myCplx); +var diffTupe2 = ( 7, 5.64, 6.0+1.5i ); + +// Accessed using array bracket notation +// However, tuples are all 1-indexed +writeln( "(", sameTup[1], ",", sameTup[2], ")" ); +writeln( diffTup ); + +// Tuples can also be written into. +diffTup[1] = -1; + +// Can expand tuple values into their own variables +var (tupInt, tupReal, tupCplx) = diffTup; +writeln( diffTup == (tupInt, tupReal, tupCplx) ); + +// Useful for writing a list of variables ( as is common in debugging) +writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); + +// Type aliasing +type chroma = int; // Type of a single hue +type RGBColor = 3*chroma; // Type representing a full color +var black: RGBColor = ( 0,0,0 ); +var white: RGBColor = ( 255, 255, 255 ); + +// If-then-else works just like any other C-family language +if 10 < 100 then + writeln( "All is well" ); + +if -1 < 1 then + writeln( "Continuing to believe reality" ); +else + writeln( "Send mathematician, something's wrong" ); + +if ( 10 > 100 ) { + writeln( "Universe broken. Please reboot universe." ); +} + +if ( a % 2 == 0 ) { + writeln( a, " is even." ); +} else { + writeln( a, " is odd." ); +} + +if ( a % 3 == 0 ) { + writeln( a, " is even divisible by 3." ); +} else if ( a % 3 == 1 ){ + writeln( a, " is divided by 3 with a remainder of 1." ); +} else { + writeln( b, " is divided by 3 with a remainder of 2." ); +} + +// Ternary: if-then-else in a statement +var maximum = if ( thisInt < thatInt ) then thatInt else thisInt; + +// Select statements are much like switch statements in other languages +// However, Select statements don't cascade like in C or Java +var inputOption = "anOption"; +select( inputOption ){ + when "anOption" do writeln( "Chose 'anOption'" ); + when "otherOption" { + writeln( "Chose 'otherOption'" ); + writeln( "Which has a body" ); + } + otherwise { + writeln( "Any other Input" ); + writeln( "the otherwise case doesn't need a do if the body is one line" ); + } +} + +// While and Do-While loops are basically the same in every language. +var j: int = 1; +var jSum: int = 0; +while( j <= 1000 ){ + jSum += j; + j += 1; +} +writeln( jSum ); + +// Do-While loop +do{ + jSum += j; + j += 1; +}while( j <= 10000 ); +writeln( jSum ); + +// For loops are much like those in python in that they iterate over a range. +// Ranges themselves are types, and can be stuffed into variables +// (more about that later) +for i in 1..10 do write( i , ", ") ; +writeln( ); + +var iSum: int = 0; +for i in 1..1000 { + iSum += i; +} +writeln( iSum ); + +for x in 1..10 { + for y in 1..10 { + write( (x,y), "\t" ); + } + writeln( ); +} + +// Ranges and Domains +// For-loops and arrays both use ranges and domains to +// define an index set that can be iterated over. +// Ranges are single dimensional +// Domains can be multi-dimensional and can +// represent indices of different types as well. +// They are first-class citizen types, and can be assigned into variables +var range1to10: range = 1..10; // 1, 2, 3, ..., 10 +var range2to11 = 2..11; // 2, 3, 4, ..., 11 +var rangeThistoThat: range = thisInt..thatInt; // using variables +var rangeEmpty: range = 100..-100 ; // this is valid but contains no indices + +// Ranges can be unbounded +var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; +// 1, 2, 3, 4, 5, ... +// Note: the range(boundedType= ... ) is only +// necessary if we explicitly type the variable + +var rangeNegInfto1 = ..1; // ..., -4, -3, -2, -1, 0, 1 + +// Ranges can be strided using the 'by' operator. +var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 +// Note: the range(stridable=true) is only +// necessary if we explicitly type the variable + +// Use by to create a reverse range +var reverse2to10by2 = 10..2 by -2; // 10, 8, 6, 4, 2 + +// The end point of a range can be determined using the count (#) operator +var rangeCount: range = -5..#12; // range from -5 to 6 + +// Can mix operators +var rangeCountBy: range(stridable=true) = -5..#12 by 2; // -5, -3, -1, 1, 3, 5 +writeln( rangeCountBy ); + +// Can query properties of the range +// Print the first index, last index, number of indices, +// stride, and ask if 2 is include in the range +writeln( ( rangeCountBy.first, rangeCountBy.last, rangeCountBy.length, + rangeCountBy.stride, rangeCountBy.member( 2 ) ) ); + +for i in rangeCountBy{ + write( i, if i == rangeCountBy.last then "\n" else ", " ); +} + +// Rectangular domains are defined using the same range syntax +var domain1to10: domain(1) = {1..10}; // 1D domain from 1..10; +var twoDimensions: domain(2) = {-2..2,0..2}; // 2D domain over product of ranges +var thirdDim: range = 1..16; +var threeDims: domain(3) = {thirdDim, 1..10, 5..10}; // using a range variable + +// Can iterate over the indices as tuples +for idx in twoDimensions do + write( idx , ", "); +writeln( ); + +// or can deconstruct the tuple +for (x,y) in twoDimensions { + write( "(", x, ", ", y, ")", ", " ); +} +writeln( ); + +// Associative domains act like sets +var stringSet: domain(string); // empty set of strings +stringSet += "a"; +stringSet += "b"; +stringSet += "c"; +stringSet += "a"; // Redundant add "a" +stringSet -= "c"; // Remove "c" +writeln( stringSet ); + +// Array are similar to those of other languages. +// Their sizes are defined using domains that represent their indices +var intArray: [1..10] int; +var intArray2: [{1..10}] int; //equivalent + +// Accessed using bracket notation +for i in 1..10 do + intArray[i] = -i; +writeln( intArray ); +// We cannot access intArray[0] because it exists outside +// of the index set, {1..10}, we defined it to have. +// intArray[11] is illegal for the same reason. + +var realDomain: domain(2) = {1..5,1..7}; +var realArray: [realDomain] real; +var realArray2: [1..5,1..7] real; // Equivalent +var realArray3: [{1..5,1..7}] real; // Equivalent + +for i in 1..5 { + for j in realDomain.dim(2) { // Only use the 2nd dimension of the domain + realArray[i,j] = -1.61803 * i + 0.5 * j; // Access using index list + var idx: 2*int = (i,j); // Note: 'index' is a keyword + realArray[idx] = - realArray[(i,j)]; // Index using tuples + } +} + +// Arrays have domains as members that we can iterate over +for idx in realArray.domain { // Again, idx is a 2*int tuple + realArray[idx] = 1 / realArray[idx[1],idx[2]]; // Access by tuple and list +} + +writeln( realArray ); + +// Can also iterate over the values of an array +var rSum: real = 0; +for value in realArray { + rSum += value; // Read a value + value = rSum; // Write a value +} +writeln( rSum, "\n", realArray ); + +// Using associative domains we can create associative arrays (dictionaries) +var dictDomain: domain(string) = { "one", "two" }; +var dict: [dictDomain] int = [ "one" => 1, "two" => 2 ]; +dict["three"] = 3; +for key in dictDomain do writeln( dict[key] ); + +// Chapel procedures have similar syntax to other languages functions. +proc fibonacci( n : int ) : int { + if ( n <= 1 ) then return n; + return fibonacci( n-1 ) + fibonacci( n-2 ); +} + +// Input parameters can be untyped (a generic procedure) +proc doublePrint( thing ): void { + write( thing, " ", thing, "\n"); +} + +// Return type can be inferred (as long as the compiler can figure it out) +proc addThree( n ) { + return n + 3; +} + +doublePrint( addThree( fibonacci( 20 ) ) ); + +// Can also take 'unlimited' number of parameters +proc maxOf( x ...?k ) { + // x refers to a tuple of one type, with k elements + var maximum = x[1]; + for i in 2..k do maximum = if (maximum < x[i]) then x[i] else maximum; + return maximum; +} +writeln( maxOf( 1, -10, 189, -9071982, 5, 17, 20001, 42 ) ); + +// The ? operator is called the query operator, and is used to take +// undetermined values (like tuple or array sizes, and generic types). + +// Taking arrays as parameters. +// The query operator is used to determine the domain of A. +// This is important to define the return type (if you wanted to) +proc invertArray( A: [?D] int ): [D] int{ + for a in A do a = -a; + return A; +} + +writeln( invertArray( intArray ) ); + +// Procedures can have default parameter values, and +// the parameters can be named in the call, even out of order +proc defaultsProc( x: int, y: real = 1.2634 ): (int,real){ + return (x,y); +} + +writeln( defaultsProc( 10 ) ); +writeln( defaultsProc( x=11 ) ); +writeln( defaultsProc( x=12, y=5.432 ) ); +writeln( defaultsProc( y=9.876, x=13 ) ); + +// Intent modifiers on the arguments convey how +// those arguments are passed to the procedure +// in: copy arg in, but not out +// out: copy arg out, but not in +// inout: copy arg in, copy arg out +// ref: pass arg by reference +proc intentsProc( in inarg, out outarg, inout inoutarg, ref refarg ){ + writeln( "Inside Before: ", (inarg, outarg, inoutarg, refarg) ); + inarg = inarg + 100; + outarg = outarg + 100; + inoutarg = inoutarg + 100; + refarg = refarg + 100; + writeln( "Inside After: ", (inarg, outarg, inoutarg, refarg) ); +} + +var inVar: int = 1; +var outVar: int = 2; +var inoutVar: int = 3; +var refVar: int = 4; +writeln( "Outside Before: ", (inVar, outVar, inoutVar, refVar) ); +intentsProc( inVar, outVar, inoutVar, refVar ); +writeln( "Outside After: ", (inVar, outVar, inoutVar, refVar) ); + +// Similarly we can define intents on the return type +// refElement returns a reference to an element of array +proc refElement( array : [?D] ?T, idx ) ref : T { + return array[ idx ]; // returns a reference to +} + +var myChangingArray : [1..5] int = [1,2,3,4,5]; +writeln( myChangingArray ); +// Store reference to element in ref variable +ref refToElem = refElement( myChangingArray, 5 ); +writeln( refToElem ); +refToElem = -2; // modify reference which modifies actual value in array +writeln( refToElem ); +writeln( myChangingArray ); +// This makes more practical sense for class methods where references to +// elements in a data-structure are returned via a method or iterator + +// We can query the type of arguments to generic procedures +// Here we define a procedure that takes two arguments of +// the same type, yet we don't define what that type is. +proc genericProc( arg1 : ?valueType, arg2 : valueType ): void { + select( valueType ){ + when int do writeln( arg1, " and ", arg2, " are ints" ); + when real do writeln( arg1, " and ", arg2, " are reals" ); + otherwise writeln( arg1, " and ", arg2, " are somethings!" ); + } +} + +genericProc( 1, 2 ); +genericProc( 1.2, 2.3 ); +genericProc( 1.0+2.0i, 3.0+4.0i ); + +// We can also enforce a form of polymorphism with the 'where' clause +// This allows the compiler to decide which function to use. +// Note: that means that all information needs to be known at compile time. +// The param modifier on the arg is used to enforce this constraint. +proc whereProc( param N : int ): void + where ( N > 0 ) { + writeln( "N is greater than 0" ); +} + +proc whereProc( param N : int ): void + where ( N < 0 ) { + writeln( "N is less than 0" ); +} + +whereProc( 10 ); +whereProc( -1 ); +// whereProc( 0 ) would result in a compiler error because there +// are no functions that satisfy the where clause's condition. +// We could have defined a whereProc without a where clause that would then have +// served as a catch all for all the other cases (of which there is only one). + +// Operator definitions are through procedures as well. +// We can define the unary operators: +// + - ! ~ +// and the binary operators: +// + - * / % ** == <= >= < > << >> & | ˆ by +// += -= *= /= %= **= &= |= ˆ= <<= >>= <=> + +// Boolean exclusive or operator +proc ^( left : bool, right : bool ): bool { + return (left || right) && !( left && right ); +} + +writeln( true ^ true ); +writeln( false ^ true ); +writeln( true ^ false ); +writeln( false ^ false ); + +// Define a * operator on any two types that returns a tupe of those types +proc *( left : ?ltype, right : ?rtype): ( ltype, rtype ){ + return (left, right ); +} + +writeln( 1 * "a" ); // Uses our * operator +writeln( 1 * 2 ); // Uses the default * operator + +/* +Note: You could break everything if you get careless with your overloads. +This here will break everything. Don't do it. +proc +( left: int, right: int ): int{ + return left - right; +} +*/ + +// Iterators are a sisters to the procedure, and almost +// everything about procedures also applies to iterators +// However, instead of returning a single value, +// iterators yield many values to a loop. +// This is useful when a complicated set or order of iterations is needed but +// allows the code defining the iterations to be separate from the loop body. +iter oddsThenEvens( N: int ): int { + for i in 1..N by 2 do + yield i; // yield values instead of returning. + for i in 2..N by 2 do + yield i; +} + +for i in oddsThenEvens( 10 ) do write( i, ", " ); +writeln( ); + +// Classes are similar to those in C++ and Java. +// They currently lack privatization +class MyClass { + // Member variables + var memberInt : int; + var memberBool : bool = true; + + // Classes have default constructors that don't need to be coded (see below) + // Our explicitly defined constructor + proc MyClass( val : real ){ + this.memberInt = ceil( val ): int; + } + + // Our explicitly defined destructor + proc ~MyClass( ){ + writeln( "MyClass Destructor called ", (this.memberInt, this.memberBool) ); + } + + // Class methods + proc setMemberInt( val: int ){ + this.memberInt = val; + } + + proc setMemberBool( val: bool ){ + this.memberBool = val; + } + + proc getMemberInt( ): int{ + return this.memberInt; + } + + proc getMemberBool( ): bool { + return this.memberBool; + } + +} + +// Construct using default constructor, using default values +var myObject = new MyClass( 10 ); + myObject = new MyClass( memberInt = 10 ); // Equivalent +writeln( myObject.getMemberInt( ) ); +// ... using our values +var myDiffObject = new MyClass( -1, true ); + myDiffObject = new MyClass( memberInt = -1, + memberBool = true ); // Equivalent +writeln( myDiffObject ); + +// Construct using written constructor +var myOtherObject = new MyClass( 1.95 ); + myOtherObject = new MyClass( val = 1.95 ); // Equivalent +writeln( myOtherObject.getMemberInt( ) ); + +// We can define an operator on our class as well but +// the definition has to be outside the class definition +proc +( A : MyClass, B : MyClass) : MyClass { + return new MyClass( memberInt = A.getMemberInt( ) + B.getMemberInt( ), + memberBool = A.getMemberBool( ) || B.getMemberBool( ) ); +} + +var plusObject = myObject + myDiffObject; +writeln( plusObject ); + +// Destruction +delete myObject; +delete myDiffObject; +delete myOtherObject; +delete plusObject; + +// Classes can inherit from one or more parent classes +class MyChildClass : MyClass { + var memberComplex: complex; +} + +// Generic Classes +class GenericClass { + type classType; + var classDomain: domain(1); + var classArray: [classDomain] classType; + + // Explicit constructor + proc GenericClass( type classType, elements : int ){ + this.classDomain = {1..#elements}; + } + + // Copy constructor + // Note: We still have to put the type as an argument, but we can + // default to the type of the other object using the query (?) operator + // Further, we can take advantage of this to allow our copy constructor + // to copy classes of different types and cast on the fly + proc GenericClass( other : GenericClass(?otherType), + type classType = otherType ) { + this.classDomain = other.classDomain; + // Copy and cast + for idx in this.classDomain do this[ idx ] = other[ idx ] : classType; + } + + // Define bracket notation on a GenericClass + // object so it can behave like a normal array + // i.e. objVar[ i ] or objVar( i ) + proc this( i : int ) ref : classType { + return this.classArray[ i ]; + } + + // Define an implicit iterator for the class + // to yield values from the array to a loop + // i.e. for i in objVar do .... + iter these( ) ref : classType { + for i in this.classDomain do + yield this[i]; + } + +} + +var realList = new GenericClass( real, 10 ); +// We can assign to the member array of the object using the bracket +// notation that we defined ( proc this( i: int ){ ... } ) +for i in realList.classDomain do realList[i] = i + 1.0; +// We can iterate over the values in our list with the iterator +// we defined ( iter these( ){ ... } ) +for value in realList do write( value, ", " ); +writeln( ); + +// Make a copy of realList using the copy constructor +var copyList = new GenericClass( realList ); +for value in copyList do write( value, ", " ); +writeln( ); + +// Make a copy of realList and change the type, also using the copy constructor +var copyNewTypeList = new GenericClass( realList, int ); +for value in copyNewTypeList do write( value, ", " ); +writeln( ); + +// Parallelism +// In other languages, parallelism is typically this is done with +// complicated libraries and strange class structure hierarchies. +// Chapel has it baked right into the language. + +// A begin statement will spin the body of that statement off into one new task. +// A sync statement will ensure that the progress of the main +// task will not progress until the children have synced back up. +sync { + begin { // Start of new task's body + var a = 0; + for i in 1..1000 do a += 1; + writeln( "Done: ", a); + } // End of new tasks body + writeln( "spun off a task!"); +} +writeln( "Back together" ); + +proc printFibb( n: int ){ + writeln( "fibonacci(",n,") = ", fibonacci( n ) ); +} + +// A cobegin statement will spin each statement of the body into one new task +cobegin { + printFibb( 20 ); // new task + printFibb( 10 ); // new task + printFibb( 5 ); // new task + { + // This is a nested statement body and thus is a single statement + // to the parent statement and is executed by a single task + writeln( "this gets" ); + writeln( "executed as" ); + writeln( "a whole" ); + } +} +// Notice here that the prints from each statement may happen in any order. + +// Coforall loop will create a new task for EACH iteration +var num_tasks = 10; // Number of tasks we want +coforall taskID in 1..#num_tasks { + writeln( "Hello from task# ", taskID ); +} +// Again we see that prints happen in any order. +// NOTE! coforall should be used only for creating tasks! +// Using it to iterating over a structure is very a bad idea! + +// forall loops are another parallel loop, but only create a smaller number +// of tasks, specifically --dataParTasksPerLocale=number of task +forall i in 1..100 { + write( i, ", "); +} +writeln( ); +// Here we see that there are sections that are in order, followed by +// a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ). +// This is because each task is taking on a chunk of the range 1..10 +// (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens +// in parallel. +// Your results may depend on your machine and configuration + +// For both the forall and coforall loops, the execution of the +// parent task will not continue until all the children sync up. + +// forall loops are particularly useful for parallel iteration over arrays. +// Lets run an experiment to see how much faster a parallel loop is +use Time; // Import the Time module to use Timer objects +var timer: Timer; +var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into +// Serial Experiment +timer.start( ); // Start timer +for (x,y) in myBigArray.domain { // Serial iteration + myBigArray[x,y] = (x:real) / (y:real); +} +timer.stop( ); // Stop timer +writeln( "Serial: ", timer.elapsed( ) ); // Print elapsed time +timer.clear( ); // Clear timer for parallel loop + +// Parallel Experiment +timer.start( ); // start timer +forall (x,y) in myBigArray.domain { // Parallel iteration + myBigArray[x,y] = (x:real) / (y:real); +} +timer.stop( ); // Stop timer +writeln( "Parallel: ", timer.elapsed( ) ); // Print elapsed time +timer.clear( ); +// You may have noticed that (depending on how many cores you have) +// that the parallel loop went faster than the serial loop + +// A succinct way of writing a forall loop over an array: +// iterate over values +[ val in myBigArray ] val = 1 / val; +// or iterate over indicies +[ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; +``` + +Who is this tutorial for? +------------------------- + +This tutorial is for people who want to learn the ropes of chapel without having to hear about what fiber mixture the ropes are, or how they were braided, or how the braid configurations differ between one another. +It won't teach you how to develop amazingly performant code, and it's not exhaustive. +Refer to the [language specification](http://chapel.cray.com/language.html) and the [library documentation](http://chapel.cray.com/docs/latest/) for more details. + +Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to see if more topics have been added or more tutorials created. + +### What this tutorial is lacking: + + * Modules and standard modules + * Synchronize variables and atomic operations + * Multiple Locales (distributed memory system) + * ```proc main(){ ... }``` + * Records + * Whole/sliced array assignment + * Reductions and scans + * Range and domain slicing + * Parallel iterators + +Your input, questions, and discoveries are important to the developers! +----------------------------------------------------------------------- + +The Chapel language is still in-development (version 1.11.0), so there are occasional hiccups with performance and language features. +The more information you give the Chapel development team about issues you encounter or features you would like to see, the better the language becomes. +Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). + +If you're really interested in the development of the compiler or contributing to the project, +[check out the master Github repository](https://github.com/chapel-lang/chapel). +It is under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). + +Installing the Compiler +----------------------- + +Chapel can be built and installed on your average 'nix machine (and cygwin). +[Download the latest release version](https://github.com/chapel-lang/chapel/releases/) +and its as easy as + + 1. ```tar -xvf chapel-1.11.0.tar.gz``` + 2. ```cd chapel-1.11.0``` + 3. ```make``` + 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` + +You will need to `source util/setchplenv.EXT` from within the Chapel directory (`$CHPL_HOME`) every time your terminal starts so its suggested that you drop that command in a script that will get executed on startup (like .bashrc). + +Chapel is easily installed with Brew for OS X + + 1. ```brew update``` + 2. ```brew install chapel``` + +Compiling Code +-------------- + +Builds like other compilers: + +```chpl myFile.chpl -o myExe``` + +A notable argument: + + * ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. diff --git a/elixir.html.markdown b/elixir.html.markdown index fb5f183a..c8599838 100644 --- a/elixir.html.markdown +++ b/elixir.html.markdown @@ -195,7 +195,7 @@ cond do "But I will" end -# It is common to see the last condition equal to `true`, which will always match. +# It is common to set the last condition equal to `true`, which will always match. cond do 1 + 1 == 3 -> "I will never be seen" diff --git a/es-es/git-es.html.markdown b/es-es/git-es.html.markdown index 5c9d3378..73853a9d 100644 --- a/es-es/git-es.html.markdown +++ b/es-es/git-es.html.markdown @@ -38,7 +38,7 @@ uno o varios archivos, a lo largo del tiempo. * Se puede trabajar sin conexion. * Colaborar con otros es sencillo!. * Derivar, Crear ramas del proyecto (aka: Branching) es facil!. -* Combinar (aka: Marging) +* Combinar (aka: Merging) * Git es rapido. * Git es flexible. diff --git a/haskell.html.markdown b/haskell.html.markdown index 6a64442f..369b1b20 100644 --- a/haskell.html.markdown +++ b/haskell.html.markdown @@ -209,14 +209,15 @@ foo 5 -- 75 -- the expression on its right is applied as the parameter to the function on its left. -- before -(even (fib 7)) -- false - --- after -even . fib $ 7 -- false +even (fib 7) -- false -- equivalently even $ fib 7 -- false +-- composing functions +even . fib $ 7 -- false + + ---------------------------------------------------- -- 5. Type signatures ---------------------------------------------------- diff --git a/matlab.html.markdown b/matlab.html.markdown index 9de41275..00f4c53a 100644 --- a/matlab.html.markdown +++ b/matlab.html.markdown @@ -43,7 +43,7 @@ edit('myfunction.m') % Open function/script in editor type('myfunction.m') % Print the source of function/script to Command Window profile on % turns on the code profiler -profile of % turns off the code profiler +profile off % turns off the code profiler profile viewer % Open profiler help command % Displays documentation for command in Command Window diff --git a/perl6.html.markdown b/perl6.html.markdown index 3bb87916..de7d2f25 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -7,10 +7,13 @@ contributors: - ["Nami-Doc", "http://github.com/Nami-Doc"] --- -Perl 6 is a highly capable, feature-rich programming language made for the -upcoming hundred years. +Perl 6 is a highly capable, feature-rich programming language made for at +least the next hundred years. -Perl 6 runs on [the MoarVM](http://moarvm.com) and the JVM. +The primary Perl 6 compiler is called [Rakudo](http://rakudo.org), which runs on +the JVM and [the MoarVM](http://moarvm.com) and +[prior to March 2015](http://pmthium.com/2015/02/suspending-rakudo-parrot/), +[the Parrot VM](http://parrot.org/). Meta-note : the triple pound signs are here to denote headlines, double paragraphs, and single notes. @@ -1041,7 +1044,7 @@ postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that) # *everything* -- with great power comes great responsibility) ## Meta operators ! -# Oh boy, get ready. Get ready, because we're dwelving deep +# Oh boy, get ready. Get ready, because we're delving deep # into the rabbit's hole, and you probably won't want to go # back to other languages after reading that. # (I'm guessing you don't want to already at that point). diff --git a/python.html.markdown b/python.html.markdown index ace3f794..88e0deb1 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -327,8 +327,8 @@ prints: mouse is a mammal """ for animal in ["dog", "cat", "mouse"]: - # You can use % to interpolate formatted strings - print "%s is a mammal" % animal + # You can use {0} to interpolate formatted strings. (See above.) + print "{0} is a mammal".format(animal) """ "range(number)" returns a list of numbers @@ -387,7 +387,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks # Use "def" to create new functions def add(x, y): - print "x is %s and y is %s" % (x, y) + print "x is {0} and y is {1}".format(x, y) return x + y # Return values with a return statement # Calling functions with parameters @@ -497,7 +497,7 @@ class Human(object): # An instance method. All methods take "self" as the first argument def say(self, msg): - return "%s: %s" % (self.name, msg) + return "{0}: {1}".format(self.name, msg) # A class method is shared among all instances # They are called with the calling class as the first argument @@ -624,16 +624,17 @@ print say(say_please=True) # Can you buy me a beer? Please! I am poor :( ### Free Online +* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com) * [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) * [Dive Into Python](http://www.diveintopython.net/) * [The Official Docs](http://docs.python.org/2.6/) * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) * [Python Module of the Week](http://pymotw.com/2/) * [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182) +* [First Steps With Python](https://realpython.com/learn/python-first-steps/) ### Dead Tree * [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) * [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) * [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) - diff --git a/python3.html.markdown b/python3.html.markdown index a112912f..9d965fb1 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -394,15 +394,15 @@ our_iterable[1] # Raises a TypeError our_iterator = iter(our_iterable) # Our iterator is an object that can remember the state as we traverse through it. -# We get the next object by calling the __next__ function. -our_iterator.__next__() #=> "one" +# We get the next object with "next()". +next(our_iterator) #=> "one" -# It maintains state as we call __next__. -our_iterator.__next__() #=> "two" -our_iterator.__next__() #=> "three" +# It maintains state as we iterate. +next(our_iterator) #=> "two" +next(our_iterator) #=> "three" # After the iterator has returned all of its data, it gives you a StopIterator Exception -our_iterator.__next__() # Raises StopIteration +next(our_iterator) # Raises StopIteration # You can grab all the elements of an iterator by calling list() on it. list(filled_dict.keys()) #=> Returns ["one", "two", "three"] @@ -642,18 +642,18 @@ print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :( ### Free Online +* [Automate the Boring Stuff with Python](https://automatetheboringstuff.com) * [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) * [Dive Into Python](http://www.diveintopython.net/) * [Ideas for Python Projects](http://pythonpracticeprojects.com) - * [The Official Docs](http://docs.python.org/3/) * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) * [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182) * [Python Course](http://www.python-course.eu/index.php) +* [First Steps With Python](https://realpython.com/learn/python-first-steps/) ### Dead Tree * [Programming Python](http://www.amazon.com/gp/product/0596158106/ref=as_li_qf_sp_asin_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596158106&linkCode=as2&tag=homebits04-20) * [Dive Into Python](http://www.amazon.com/gp/product/1441413022/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1441413022&linkCode=as2&tag=homebits04-20) * [Python Essential Reference](http://www.amazon.com/gp/product/0672329786/ref=as_li_tf_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0672329786&linkCode=as2&tag=homebits04-20) - diff --git a/rust.html.markdown b/rust.html.markdown index fbfa4cdf..dd03acdd 100644 --- a/rust.html.markdown +++ b/rust.html.markdown @@ -5,7 +5,7 @@ contributors: filename: learnrust.rs --- -Rust is an in-development programming language developed by Mozilla Research. +Rust is a programming language developed by Mozilla Research. Rust combines low-level control over performance with high-level convenience and safety guarantees. diff --git a/zh-cn/groovy-cn.html.markdown b/zh-cn/groovy-cn.html.markdown new file mode 100644 index 00000000..562a0284 --- /dev/null +++ b/zh-cn/groovy-cn.html.markdown @@ -0,0 +1,420 @@ +--- +language: Groovy +filename: learngroovy-cn.groovy +contributors: + - ["Roberto Pérez Alcolea", "http://github.com/rpalcolea"] +translators: + - ["Todd Gao", "http://github.com/7c00"] +lang: zh-cn +--- + +Groovy - Java平台的动态语言。[了解更多。](http://www.groovy-lang.org/) + +```groovy + +/* + 安装: + + 1) 安装 GVM - http://gvmtool.net/ + 2) 安装 Groovy: gvm install groovy + 3) 启动 groovy 控制台,键入: groovyConsole + +*/ + +// 双斜线开始的是单行注释 +/* +像这样的是多行注释 +*/ + +// Hello World +println "Hello world!" + +/* + 变量: + + 可以给变量赋值,以便稍后使用 +*/ + +def x = 1 +println x + +x = new java.util.Date() +println x + +x = -3.1499392 +println x + +x = false +println x + +x = "Groovy!" +println x + +/* + 集合和映射 +*/ + +//创建一个空的列表 +def technologies = [] + +/*** 往列表中增加一个元素 ***/ + +// 和Java一样 +technologies.add("Grails") + +// 左移添加,返回该列表 +technologies << "Groovy" + +// 增加多个元素 +technologies.addAll(["Gradle","Griffon"]) + +/*** 从列表中删除元素 ***/ + +// 和Java一样 +technologies.remove("Griffon") + +// 减号也行 +technologies = technologies - 'Grails' + +/*** 遍历列表 ***/ + +// 遍历列表中的元素 +technologies.each { println "Technology: $it"} +technologies.eachWithIndex { it, i -> println "$i: $it"} + +/*** 检查列表内容 ***/ + +//判断列表是否包含某元素,返回boolean +contained = technologies.contains( 'Groovy' ) + +// 或 +contained = 'Groovy' in technologies + +// 检查多个元素 +technologies.containsAll(['Groovy','Grails']) + +/*** 列表排序 ***/ + +// 排序列表(修改原列表) +technologies.sort() + +// 要想不修改原列表,可以这样: +sortedTechnologies = technologies.sort( false ) + +/*** 列表操作 ***/ + +//替换列表元素 +Collections.replaceAll(technologies, 'Gradle', 'gradle') + +//打乱列表 +Collections.shuffle(technologies, new Random()) + +//清空列表 +technologies.clear() + +//创建空的映射 +def devMap = [:] + +//增加值 +devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] +devMap.put('lastName','Perez') + +//遍历映射元素 +devMap.each { println "$it.key: $it.value" } +devMap.eachWithIndex { it, i -> println "$i: $it"} + +//判断映射是否包含某键 +assert devMap.containsKey('name') + +//判断映射是否包含某值 +assert devMap.containsValue('Roberto') + +//取得映射所有的键 +println devMap.keySet() + +//取得映射所有的值 +println devMap.values() + +/* + Groovy Beans + + GroovyBeans 是 JavaBeans,但使用了更简单的语法 + + Groovy 被编译为字节码时,遵循下列规则。 + + * 如果一个名字声明时带有访问修饰符(public, private, 或者 protected), + 则会生成一个字段(field)。 + + * 名字声明时没有访问修饰符,则会生成一个带有public getter和setter的 + private字段,即属性(property)。 + + * 如果一个属性声明为final,则会创建一个final的private字段,但不会生成setter。 + + * 可以声明一个属性的同时定义自己的getter和setter。 + + * 可以声明具有相同名字的属性和字段,该属性会使用该字段。 + + * 如果要定义private或protected属性,必须提供声明为private或protected的getter + 和setter。 + + * 如果使用显式或隐式的 this(例如 this.foo, 或者 foo)访问类的在编译时定义的属性, + Groovy会直接访问对应字段,而不是使用getter或者setter + + * 如果使用显式或隐式的 foo 访问一个不存在的属性,Groovy会通过元类(meta class) + 访问它,这可能导致运行时错误。 + +*/ + +class Foo { + // 只读属性 + final String name = "Roberto" + + // 只读属性,有public getter和protected setter + String language + protected void setLanguage(String language) { this.language = language } + + // 动态类型属性 + def lastName +} + +/* + 逻辑分支和循环 +*/ + +//Groovy支持常见的if - else语法 +def x = 3 + +if(x==1) { + println "One" +} else if(x==2) { + println "Two" +} else { + println "X greater than Two" +} + +//Groovy也支持三元运算符 +def y = 10 +def x = (y > 1) ? "worked" : "failed" +assert x == "worked" + +//for循环 +//使用区间(range)遍历 +def x = 0 +for (i in 0 .. 30) { + x += i +} + +//遍历列表 +x = 0 +for( i in [5,3,2,1] ) { + x += i +} + +//遍历数组 +array = (0..20).toArray() +x = 0 +for (i in array) { + x += i +} + +//遍历映射 +def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy'] +x = 0 +for ( e in map ) { + x += e.value +} + +/* + 运算符 + + 在Groovy中以下常用运算符支持重载: + http://www.groovy-lang.org/operators.html#Operator-Overloading + + 实用的groovy运算符 +*/ +//展开(spread)运算符:对聚合对象的所有元素施加操作 +def technologies = ['Groovy','Grails','Gradle'] +technologies*.toUpperCase() // 相当于 technologies.collect { it?.toUpperCase() } + +//安全导航(safe navigation)运算符:用来避免NullPointerException +def user = User.get(1) +def username = user?.username + + +/* + 闭包 + Groovy闭包好比代码块或者方法指针,它是一段代码定义,可以以后执行。 + + 更多信息见:http://www.groovy-lang.org/closures.html +*/ +//例子: +def clos = { println "Hello World!" } + +println "Executing the Closure:" +clos() + +//传参数给闭包 +def sum = { a, b -> println a+b } +sum(2,4) + +//闭包可以引用参数列表以外的变量 +def x = 5 +def multiplyBy = { num -> num * x } +println multiplyBy(10) + +// 只有一个参数的闭包可以省略参数的定义 +def clos = { print it } +clos( "hi" ) + +/* + Groovy可以记忆闭包结果 [1][2][3] +*/ +def cl = {a, b -> + sleep(3000) // 模拟费时操作 + a + b +} + +mem = cl.memoize() + +def callClosure(a, b) { + def start = System.currentTimeMillis() + mem(a, b) + println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs." +} + +callClosure(1, 2) +callClosure(1, 2) +callClosure(2, 3) +callClosure(2, 3) +callClosure(3, 4) +callClosure(3, 4) +callClosure(1, 2) +callClosure(2, 3) +callClosure(3, 4) + +/* + Expando + + Expando类是一种动态bean类,可以给它的实例添加属性和添加闭包作为方法 + + http://mrhaki.blogspot.mx/2009/10/groovy-goodness-expando-as-dynamic-bean.html +*/ + def user = new Expando(name:"Roberto") + assert 'Roberto' == user.name + + user.lastName = 'Pérez' + assert 'Pérez' == user.lastName + + user.showInfo = { out -> + out << "Name: $name" + out << ", Last name: $lastName" + } + + def sw = new StringWriter() + println user.showInfo(sw) + + +/* + 元编程(MOP) +*/ + +//使用ExpandoMetaClass增加行为 +String.metaClass.testAdd = { + println "we added this" +} + +String x = "test" +x?.testAdd() + +//拦截方法调用 +class Test implements GroovyInterceptable { + def sum(Integer x, Integer y) { x + y } + + def invokeMethod(String name, args) { + System.out.println "Invoke method $name with args: $args" + } +} + +def test = new Test() +test?.sum(2,3) +test?.multiply(2,3) + +//Groovy支持propertyMissing,来处理属性解析尝试 +class Foo { + def propertyMissing(String name) { name } +} +def f = new Foo() + +assertEquals "boo", f.boo + +/* + 类型检查和静态编译 + Groovy天生是并将永远是一门动态语言,但也支持类型检查和静态编译 + + 更多: http://www.infoq.com/articles/new-groovy-20 +*/ +//类型检查 +import groovy.transform.TypeChecked + +void testMethod() {} + +@TypeChecked +void test() { + testMeethod() + + def name = "Roberto" + + println naameee + +} + +//另一例子 +import groovy.transform.TypeChecked + +@TypeChecked +Integer test() { + Integer num = "1" + + Integer[] numbers = [1,2,3,4] + + Date date = numbers[1] + + return "Test" + +} + +//静态编译例子 +import groovy.transform.CompileStatic + +@CompileStatic +int sum(int x, int y) { + x + y +} + +assert sum(2,5) == 7 + + +``` + +## 进阶资源 + +[Groovy文档](http://www.groovy-lang.org/documentation.html) + +[Groovy web console](http://groovyconsole.appspot.com/) + +加入[Groovy用户组](http://www.groovy-lang.org/usergroups.html) + +## 图书 + +* [Groovy Goodness] (https://leanpub.com/groovy-goodness-notebook) + +* [Groovy in Action] (http://manning.com/koenig2/) + +* [Programming Groovy 2: Dynamic Productivity for the Java Developer] (http://shop.oreilly.com/product/9781937785307.do) + +[1] http://roshandawrani.wordpress.com/2010/10/18/groovy-new-feature-closures-can-now-memorize-their-results/ +[2] http://www.solutionsiq.com/resources/agileiq-blog/bid/72880/Programming-with-Groovy-Trampoline-and-Memoize +[3] http://mrhaki.blogspot.mx/2011/05/groovy-goodness-cache-closure-results.html + + + diff --git a/zh-cn/rust-cn.html.markdown b/zh-cn/rust-cn.html.markdown new file mode 100644 index 00000000..17a897df --- /dev/null +++ b/zh-cn/rust-cn.html.markdown @@ -0,0 +1,296 @@ +--- +language: rust +contributors: + - ["P1start", "http://p1start.github.io/"] +translators: + - ["Guangming Mao", "http://maogm.com"] +filename: learnrust-cn.rs +lang: zh-cn +--- + +Rust 是由 Mozilla 研究院开发的编程语言。Rust 将底层的性能控制与高级语言的便利性和安全保障结合在了一起。 + +而 Rust 并不需要一个垃圾回收器或者运行时即可实现这个目的,这使得 Rust 库可以成为一种 C 语言的替代品。 + +Rust 第一版(0.1 版)发布于 2012 年 1 月,3 年以来一直在紧锣密鼓地迭代。 +因为更新太频繁,一般建议使用每夜构建版而不是稳定版,直到最近 1.0 版本的发布。 + +2015 年 3 月 15 日,Rust 1.0 发布,完美向后兼容,最新的每夜构建版提供了缩短编译时间等新特性。 +Rust 采用了持续迭代模型,每 6 周一个发布版。Rust 1.1 beta 版在 1.0 发布时同时发布。 + +尽管 Rust 相对来说是一门底层语言,它提供了一些常见于高级语言的函数式编程的特性。这让 Rust 不仅高效,并且易用。 + +```rust +// 这是注释,单行注释... +/* ...这是多行注释 */ + +/////////////// +// 1. 基础 // +/////////////// + +// 函数 (Functions) +// `i32` 是有符号 32 位整数类型(32-bit signed integers) +fn add2(x: i32, y: i32) -> i32 { + // 隐式返回 (不要分号) + x + y +} + +// 主函数(Main function) +fn main() { + // 数字 (Numbers) // + + // 不可变绑定 + let x: i32 = 1; + + // 整形/浮点型数 后缀 + let y: i32 = 13i32; + let f: f64 = 1.3f64; + + // 类型推导 + // 大部分时间,Rust 编译器会推导变量类型,所以不必把类型显式写出来。 + // 这个教程里面很多地方都显式写了类型,但是只是为了示范。 + // 绝大部分时间可以交给类型推导。 + let implicit_x = 1; + let implicit_f = 1.3; + + // 算术运算 + let sum = x + y + 13; + + // 可变变量 + let mut mutable = 1; + mutable = 4; + mutable += 2; + + // 字符串 (Strings) // + + // 字符串字面量 + let x: &str = "hello world!"; + + // 输出 + println!("{} {}", f, x); // 1.3 hello world + + // 一个 `String` – 在堆上分配空间的字符串 + let s: String = "hello world".to_string(); + + // 字符串分片(slice) - 另一个字符串的不可变视图 + // 基本上就是指向一个字符串的不可变指针,它不包含字符串里任何内容,只是一个指向某个东西的指针 + // 比如这里就是 `s` + let s_slice: &str = &s; + + println!("{} {}", s, s_slice); // hello world hello world + + // 数组 (Vectors/arrays) // + + // 长度固定的数组 (array) + let four_ints: [i32; 4] = [1, 2, 3, 4]; + + // 变长数组 (vector) + let mut vector: Vec<i32> = vec![1, 2, 3, 4]; + vector.push(5); + + // 分片 - 某个数组(vector/array)的不可变视图 + // 和字符串分片基本一样,只不过是针对数组的 + let slice: &[i32] = &vector; + + // 使用 `{:?}` 按调试样式输出 + println!("{:?} {:?}", vector, slice); // [1, 2, 3, 4, 5] [1, 2, 3, 4, 5] + + // 元组 (Tuples) // + + // 元组是固定大小的一组值,可以是不同类型 + let x: (i32, &str, f64) = (1, "hello", 3.4); + + // 解构 `let` + let (a, b, c) = x; + println!("{} {} {}", a, b, c); // 1 hello 3.4 + + // 索引 + println!("{}", x.1); // hello + + ////////////// + // 2. 类型 (Type) // + ////////////// + + // 结构体(Sturct) + struct Point { + x: i32, + y: i32, + } + + let origin: Point = Point { x: 0, y: 0 }; + + // 匿名成员结构体,又叫“元组结构体”(‘tuple struct’) + struct Point2(i32, i32); + + let origin2 = Point2(0, 0); + + // 基础的 C 风格枚举类型(enum) + enum Direction { + Left, + Right, + Up, + Down, + } + + let up = Direction::Up; + + // 有成员的枚举类型 + enum OptionalI32 { + AnI32(i32), + Nothing, + } + + let two: OptionalI32 = OptionalI32::AnI32(2); + let nothing = OptionalI32::Nothing; + + // 泛型 (Generics) // + + struct Foo<T> { bar: T } + + // 这个在标准库里面有实现,叫 `Option` + enum Optional<T> { + SomeVal(T), + NoVal, + } + + // 方法 (Methods) // + + impl<T> Foo<T> { + // 方法需要一个显式的 `self` 参数 + fn get_bar(self) -> T { + self.bar + } + } + + let a_foo = Foo { bar: 1 }; + println!("{}", a_foo.get_bar()); // 1 + + // 接口(Traits) (其他语言里叫 interfaces 或 typeclasses) // + + trait Frobnicate<T> { + fn frobnicate(self) -> Option<T>; + } + + impl<T> Frobnicate<T> for Foo<T> { + fn frobnicate(self) -> Option<T> { + Some(self.bar) + } + } + + let another_foo = Foo { bar: 1 }; + println!("{:?}", another_foo.frobnicate()); // Some(1) + + /////////////////////////////////// + // 3. 模式匹配 (Pattern matching) // + /////////////////////////////////// + + let foo = OptionalI32::AnI32(1); + match foo { + OptionalI32::AnI32(n) => println!("it’s an i32: {}", n), + OptionalI32::Nothing => println!("it’s nothing!"), + } + + // 高级模式匹配 + struct FooBar { x: i32, y: OptionalI32 } + let bar = FooBar { x: 15, y: OptionalI32::AnI32(32) }; + + match bar { + FooBar { x: 0, y: OptionalI32::AnI32(0) } => + println!("The numbers are zero!"), + FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m => + println!("The numbers are the same"), + FooBar { x: n, y: OptionalI32::AnI32(m) } => + println!("Different numbers: {} {}", n, m), + FooBar { x: _, y: OptionalI32::Nothing } => + println!("The second number is Nothing!"), + } + + /////////////////////////////// + // 4. 流程控制 (Control flow) // + /////////////////////////////// + + // `for` 循环 + let array = [1, 2, 3]; + for i in array.iter() { + println!("{}", i); + } + + // 区间 (Ranges) + for i in 0u32..10 { + print!("{} ", i); + } + println!(""); + // 输出 `0 1 2 3 4 5 6 7 8 9 ` + + // `if` + if 1 == 1 { + println!("Maths is working!"); + } else { + println!("Oh no..."); + } + + // `if` 可以当表达式 + let value = if true { + "good" + } else { + "bad" + }; + + // `while` 循环 + while 1 == 1 { + println!("The universe is operating normally."); + } + + // 无限循环 + loop { + println!("Hello!"); + } + + //////////////////////////////////////////////// + // 5. 内存安全和指针 (Memory safety & pointers) // + //////////////////////////////////////////////// + + // 独占指针 (Owned pointer) - 同一时刻只能有一个对象能“拥有”这个指针 + // 意味着 `Box` 离开他的作用域后,会被安全地释放 + let mut mine: Box<i32> = Box::new(3); + *mine = 5; // 解引用 + // `now_its_mine` 获取了 `mine` 的所有权。换句话说,`mine` 移动 (move) 了 + let mut now_its_mine = mine; + *now_its_mine += 2; + + println!("{}", now_its_mine); // 7 + // println!("{}", mine); // 编译报错,因为现在 `now_its_mine` 独占那个指针 + + // 引用 (Reference) – 引用其他数据的不可变指针 + // 当引用指向某个值,我们称为“借用”这个值,因为是被不可变的借用,所以不能被修改,也不能移动 + // 借用一直持续到生命周期结束,即离开作用域 + let mut var = 4; + var = 3; + let ref_var: &i32 = &var; + + println!("{}", var); //不像 `box`, `var` 还可以继续使用 + println!("{}", *ref_var); + // var = 5; // 编译报错,因为 `var` 被借用了 + // *ref_var = 6; // 编译报错,因为 `ref_var` 是不可变引用 + + // 可变引用 (Mutable reference) + // 当一个变量被可变地借用时,也不可使用 + let mut var2 = 4; + let ref_var2: &mut i32 = &mut var2; + *ref_var2 += 2; + + println!("{}", *ref_var2); // 6 + // var2 = 2; // 编译报错,因为 `var2` 被借用了 +} +``` + +## 更深入的资料 + +Rust 还有很多很多其他内容 - 这只是 Rust 最基本的功能,帮助你了解 Rust 里面最重要的东西。 +如果想深入学习 Rust,可以去读 +[The Rust Programming Language](http://doc.rust-lang.org/book/index.html) +或者上 reddit [/r/rust](http://reddit.com/r/rust) 订阅。 +同时 irc.mozilla.org 的 #rust 频道上的小伙伴们也非常欢迎新来的朋友。 + +你可以在这个在线编译器 [Rust playpen](http://play.rust-lang.org) 上尝试 Rust 的一些特性 +或者上[官方网站](http://rust-lang.org). |