Logo

Programming-Idioms

Declare an optional integer argument x to procedure f, printing out "Present" and its value if it is present, "Not present" otherwise
Implementation
Rust

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Rust implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
subroutine f(x)
  integer, optional :: x
  if (present(x)) then
    print *,"Present", x
  else
    print *,"Not present"
  end if
end subroutine f
   
procedure f; overload;
begin
  writeln('not present');
end;

procedure f(x: integer); overload;
begin
  writeln('present');
end;
def f( x=nil )
  puts x ? "present" : "not present"
end
def f(x=None):
    if x is None:
        print("Not present")
    else:
        print("Present", x)
function f(?int $x = null) {
    echo $x ? 'Present' . $x : 'Not present';
}
#include <optional>
#include <iostream>
void f(std::optional<int> x = {}) {
  std::cout << (x ? "Present" + std::to_string(x.value()) : "Not Present");
}
#include <optional>
#include <iostream>
void f(std::optional<int> x = {}) {
  if (x) {
    std::cout << "Present" << x.value();
  } else {
    std::cout << "Not present";
  }
}
function f( x )
	if x then
		print("Present", x)
	else
		print("Not present")
	end
end
function f(x) {
	console.log(x ? `Present: ${x}` : 'Not present');
}
using System;
void f(int? x = null)
{
    Console.WriteLine(x.HasValue ? $"Present {x}" : "Not Present");
}
func f(x ...int) {
	if len(x) > 0 {
		println("Present", x[0])
	} else {
		println("Not present")
	}
}
(defn f 
  ([] (println "Not present"))
  ([x] (println "Present" x)))
(defn f [& [x]]
  (if (integer? x)
    (println "Present" x)
    (println "Not present")))
Imports System
Sub f(Optional x As Integer? = Nothing)
    Console.WriteLine(If(x.HasValue, $"Present {x}", "Not Present"))
End Sub
sub f {
    my $x = shift;
    if (defined $x) {
        print("Present $x\n");
    }
    else {
        print("Not Present\n");
    }
}
void f({int? x}) => print(x == null ? "Not present" : "Present");
private void f(Integer x) {
    if (x != null) {
        System.out.println("Present " + x);
    } else {
        System.out.println("Not present");
    }
}