I have received numerous requests to cover the Rust programming language because there is very little information on it and really no books. So, in this tutorial I’ll provide an introduction to the Rust programming language. We’ll be covering Primitives, Output, Math Functions, Conditionals, Looping, Strings, Input, Arrays, Vectors, Tuples, Functions, Closures, Pointers, Structs, Traits, Enums and a whole lot more.

All of the code and a transcript of the video follows below.

If you like videos like this, consider supporting me with a $1 on Patreon.

Code From the Video

Rust Tutorial

Rust

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

/*

Install Rust on Mac by typing the following in a terminal

curl -sSf https://static.rust-lang.org/rustup.sh | sh

Install using an installer for Windows

https://www.rust-lang.org/downloads.html

Type the following to verify install rustc --version

Install Rust support in Atom

Packages -> Command Palette -> Toggle

Type install -> Install Packages and Themes

atom-language-rust -> Click Install

Compile with rustc rusttut.rs -A warnings

Run ./rusttut or rusttut.exe on Windows

*/

// Import data types for testing

usestd::{i8,i16,i32,i64,u8,u16,u32,u64,isize,usize,f32,f64};

usestd::io::stdin;

// The main function executes when you run the program

fnmain(){

// This macro prints to the screen

println!("Hello World");

// let defines a variable

// The data type will be guessed if not provided

// Variable values are immutable (Can't change)

letnum=10;

// Define a 32 bit mutable integer

letmutage:i32=40;

// There are many number types i8, i16, i32,

// i64, u8, u16, u32, u64, isize, usize, f32, f64

println!("Max i8 {}",i8::MAX);

println!("Min i8 {}",i8::MIN);

println!("Max i16 {}",i16::MAX);

println!("Min i16 {}",i16::MIN);

println!("Max i32 {}",i32::MAX);

println!("Min i32 {}",i32::MIN);

println!("Max i64 {}",i64::MAX);

println!("Min i64 {}",i64::MIN);

println!("Max isize {}",isize::MAX);

println!("Min isize {}",isize::MIN);

println!("Max usize {}",usize::MAX);

println!("Min usize {}",usize::MIN);

println!("Max f32 {}",f32::MAX);

println!("Min f32 {}",f32::MIN);

println!("Max f64 {}",f64::MAX);

println!("Min f64 {}",f64::MIN);

// There are booleans

letis_it_true:bool=true;

// Characters

letlet_x:char='x';

// Place variable values in output

println!("I am {} years old",age);

// You can define multiple variables

let(f_name,l_name)=("Derek","Banas");

// ---------- OUTPUT ----------

// You can place data multiple times

println!("It is {0} that {1} is {0}",

is_it_true,let_x);

// Format output

println!("{:.2}",1.234);

println!("B: {:b} H: {:x} O: {:o}",10,10,10);

// Use named arguments

// Define whitespace before data

println!("{ten:>ws$}",ten=10,ws=5);

// Pad output with zeros

println!("{ten:>0ws$}",ten=10,ws=5);

// ---------- MATH ----------

println!("5 + 4 = {}",5+4);

println!("5 - 4 = {}",5-4);

println!("5 * 4 = {}",5*4);

println!("5 / 4 = {}",5/4);

println!("5 % 4 = {}",5%4);

letmutneg_4=-4i32;

println!("abs(-4) = {}",neg_4.abs());

println!("4 ^ 6 = {}",4i32.pow(6));

println!("sqrt 9 = {}",9f64.sqrt());

println!("cbrt 9 = {}",27f64.cbrt());

println!("Round 1.45 = {}",1.45f64.round());

println!("Floor 1.45 = {}",1.45f64.floor());

println!("Ceiling 1.45 = {}",1.45f64.ceil());

println!("e ^ 2 = {}",2f64.exp());

println!("log(2) = {}",2f64.ln());

println!("log10(2) = {}",2f64.log10());

println!("90 to Radians = {}",90f64.to_radians());

println!("PI to Degrees = {}",3.14f64.to_degrees());

println!("Max 4, 5 = {}",4f64.max(5f64));

println!("Min 4, 5 = {}",4f64.min(5f64));

// sin, cos, tan, asin, acos, atan, atan2, sinh,

// cosh, tanh

println!("Sin 3.14 = {}",3.14f64.sin());

// ---------- CONDITIONALS ----------

//

letage_old=6;

if(age_old==5){

println!("Go to kindergarten");

}elseif(age_old>5)&&(age_old<=18){

println!("Go to grade {}",(age_old-5));

}elseif(age_old<=25)&&(age_old>18){

println!("Go to college");

}else{

println!("Do what you want");

}

println!("!true = {}",!true);

println!("true || false = {}",true||false);

println!("true != false : {}",(true!=false));

// Ternary operator

letcan_vote=if(age_old>=18){true}else{false};

println!("Can Vote : {}",can_vote);

// ---------- LOOPING ----------

letmutx=1;

loop{

// If even print number

if((x%2)==0){

println!("{}",x);

x+=1;

// Jump back to the beginning of the loop

continue;

}

if(x>10){

// Jump out of the loop

break;

}

x+=1;

continue;

}

letmuty=1;

whiley<=10{

println!("WHILE : {}",y);

y+=1;

}

forzin1..10{

println!("FOR : {}",z);

}

// ---------- STRINGS ----------

letrand_string="I am a random string";

// String length

println!("Length : {}",rand_string.len());

// Split a string in half at index

let(first,second)=rand_string.split_at(6);

println!("First : {} Second : {}",first,second);

// Return an iterator for the string

letcount=rand_string.chars().count();

letmutchars=rand_string.chars();

letmutindiv_char=chars.next();

loop{

// Pattern match like switch

matchindiv_char{

// If show print

Some(x)=>println!("{}",x),

// If None break

None=>break,

}

indiv_char=chars.next();

}

// Split on whitespace

letmutiter=rand_string.split_whitespace();

letmutindiv_word=iter.next();

loop{

matchindiv_word{

Some(x)=>println!("{}",x),

None=>break,

}

indiv_word=iter.next();

}

// Iterate over lines of string

letrand_string2="I am a random string\nThere are other strings like it\nThis string is the best";